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());

 

转载于:https://www.cnblogs.com/wq3435/p/6153997.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值