java高新技术-操作javaBean

1. 对javaBean的简单内省操作

public class IntroSpectorTest {

    public static void main(String[] args) throws Exception{
        ReflectPoint pt1 = new ReflectPoint(3, 5);
        
        String propertyName = "x";
        //"x" --> "X" -->"getX" --> MethodGetX-->
        Object retVal = getProperty(pt1, propertyName);
        System.out.println(retVal);
        
        Object value = 7;
        setProperties(pt1, propertyName, value);
        
        System.out.println(pt1.getX());
    }
    
    private static Object getProperty(Object pt1, String propertyName) throws Exception{
        PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());
        Method methodGetX = pd.getReadMethod();
        Object retVal = methodGetX.invoke(pt1);
        return retVal;
    }
    
    private static void setProperties(Object pt1, String propertyName, Object value) throws Exception{
        PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());
        Method methodSetX = pd.getWriteMethod();
        methodSetX.invoke(pt1, value);
    }
}

 采用复杂的一种方式

private static Object getProperty(Object pt1, String propertyName) throws Exception{
//        PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());
//        Method methodGetX = pd.getReadMethod();
//        Object retVal = methodGetX.invoke(pt1);
        
        //采用复杂的方式
        //把一个普通类当作javaBean来看待
        BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        Object retVal = null;
        for(PropertyDescriptor pd : propertyDescriptors){
            if(pd.getName().equals(propertyName)){
                Method readMethod = pd.getReadMethod();
                retVal = readMethod.invoke(pt1);
                break;
            }
        }
        return retVal;
    }

2.使用BeanUtils 工具包操作javaBean

/**
         * 使用BeanUtils
         * 设置属性和获取值使用的是字符串,1.自动完成类型的转换
         */
        System.out.println(BeanUtils.getProperty(pt1, "x").getClass().getName());
        BeanUtils.setProperty(pt1, "x", "9");
        System.out.println(pt1.getX());
        
        //2.支持属性的级联操作
        BeanUtils.setProperty(pt1, "birthday.time", "111");
        System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));

查看帮助文档:/apache-commons/commons-beanutils-1.8.0-bin/commons-beanutils-1.8.0/apidocs/index.html

可以把一个对象的属性拷贝到另一个对象上去

 

static void    copyProperties(Object dest, Object orig) 
          Copy property values from the origin bean to the destination bean for all cases where the property names are the same.

 

 可以把javaBean的属性转换为一个Map

 

static Map    describe(Object bean) 
          Return the entire set of properties for which the specified bean provides a read method.

 

把Map填充到javaBean中去

 

static void    populate(Object bean, Map properties) 
          Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs

 

 

BeanUtils是以字符串的形式对javaBean进行操作,

PropertyUtils 是以bean本身的类型对javaBean进行操作

 

//使用PropertyUtils
        PropertyUtils.setProperty(pt1, "x", 9);
        System.out.println(PropertyUtils.getProperty(pt1, "x").getClass()
                .getName());

 

posted @ 2016-12-10 01:03  wq3435  阅读(285)  评论(0编辑  收藏  举报