码迷,mamicode.com
首页 > 编程语言 > 详细

通过内省机制设置JavaBean

时间:2014-09-04 22:02:50      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   使用   java   div   sp   

一、步骤:

  1)使用PropertyDescriptor类获取属性描述者对象

//pd引用Student的name属性
PropertyDescriptor pd = new PropertyDescriptor("name", Student.class);

 

  2)通过getWriteMethod()方法+invoke()方法进行设置

//相当于得到setName()
Method m = pd.getWriteMethod();        
m.invoke(stu, "berrry");

 

  3)通过getReadMethod()方法+invoke()方法进行获取

//相当于得到getName()
m = pd.getReadMethod();
String returnValue = (String) m.invoke(s, null);

 

二、通过BeanInfo来遍历所有的属性并进行设置

 1)通过Introspector类的静态方法getBeanInfo()方法获取BeanInfo对象

//BeanInfo表示该Student对象所有的属性集合对象
BeanInfo bi = Introspector.getBeanInfo(Student.class);

  2)获取所有属性集合

//取得Student对象所有属性集合
PropertyDescriptor[] pds = bi.getPropertyDescriptors();

 3)如上:通过getWriteMethod()和getReadMethod() + invoke()方法进行相应的操作。

 

通过内省机制设置JavaBean

标签:des   style   blog   color   os   使用   java   div   sp   

原文地址:http://www.cnblogs.com/SkyGood/p/3956843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!