javaBean Getter与Setter方法的内省调用

来源:互联网 发布:linux目录结构图 编辑:程序博客网 时间:2024/06/11 12:40

我们知道在进行javaEE开发的时候我们很多 的java对象都是按照一定的格式来进行书写如字段用私有并通过getter,setter方法来访问。这样的特殊的java对象就是javaBena。


setAge() 对应age字段

setage()对应age字段

setAGE() 对应AGE自动


 我们在进行框架设计的时候很多的字段用户都会按照标准提供对应的getter,setter方法,那我们如果是通过字符串拼接的方式来拼接出来方法的名字那就太out了。

javaAPI中给我们提供了两种的方法来进行获取对应对象中某个字段的getter,setter方法。都是返回Method对象。

方法1.

通过new PropertyDescriptor(字段名,class对象);来得到参数描述对象从而得到对应的getter,setter方法。

方法2.

通过获取BenaInfo对象来得到PropertyDescriptor对象在得到getter,setter



第一我创建一个BBB类

import java.util.Date;public class BBB{<span style="white-space:pre"></span>public BBB(){};<span style="white-space:pre"></span>public BBB(String age) {<span style="white-space:pre"><span style="white-space:pre"></span></span>super();<span style="white-space:pre"></span>this.age = age;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>private String age;<span style="white-space:pre"></span>private int x;<span style="white-space:pre"></span>private Date date=new Date();<span style="white-space:pre"></span>public String getAge() {<span style="white-space:pre"></span>return age;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void setAge(String age) {<span style="white-space:pre"></span>this.age = age;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public int getX() {<span style="white-space:pre"></span>return x;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void setX(int x) {<span style="white-space:pre"></span>this.x = x;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public Date getDate() {<span style="white-space:pre"></span>return date;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void setDate(Date date) {<span style="white-space:pre"></span>this.date = date;<span style="white-space:pre"></span>}}

创建测试类:


public class testGS {/** * @param args */public static void main(String[] args) throws Exception{// TODO Auto-generated method stubBBB b = new BBB("3");String name = "age";String value = "16";setProperties(b, name, value);Object age = getProperties(b, name);System.out.println(age);                //通过开源项目提供的Beanutils来读取getter,setter方法                System.out.println(BeanUtils.getProperty(bb, "x").getClass().getName());//java.lang.String               <span style="white-space:pre"></span>System.out.println(PropertyUtils.getProperty(bb, "x").getClass().getName());//java.lang.Integer<span style="white-space:pre"></span>BeanUtils.setProperty(bb, "date.time", "111");//给date属性(Date)time赋值}private static Object getProperties(BBB b, String name)throws IntrospectionException, IllegalAccessException,InvocationTargetException {/*PropertyDescriptor p = new PropertyDescriptor(name, b.getClass());//通过内省的方法来执行制定字段的get方法Method mathGet = p.getReadMethod();mathGet.setAccessible(true);Object age = mathGet.invoke(b);*///方法2BeanInfo beanInfo = Introspector.getBeanInfo(b.getClass());PropertyDescriptor[] mathAll = beanInfo.getPropertyDescriptors();Object age = null;for(PropertyDescriptor pd:mathAll){if(pd.getName().equals(name)){Method mathGet = pd.getReadMethod();mathGet.setAccessible(true);age = mathGet.invoke(b);}}return age;}private static void setProperties(Object b, String name, String value)throws IntrospectionException, IllegalAccessException,InvocationTargetException {/*方法1PropertyDescriptor p1 = new PropertyDescriptor(name, b.getClass());//通过内省方式给age字段赋值 自动调用setAge方法Method mathSet = p1.getWriteMethod();mathSet.invoke(b, value);*///方法2BeanInfo beanInfo = Introspector.getBeanInfo(b.getClass());//通过bean信息对象获取到该对象的所有属性描述PropertyDescriptor[] mathAll = beanInfo.getPropertyDescriptors();for(PropertyDescriptor pd:mathAll){if(pd.getName().equals(name)){Method mathSet = pd.getWriteMethod();mathSet.invoke(b, value);}}}}


备注:

BeanUtils类是阿帕奇的开源项目 commons-beanutils-1.8.3.jar这个jar包,同时要依赖commons-logging-1.1.3.jar包

在操作BBB类时类必须是public修饰不让找不到方法。



原创粉丝点击