javaBean Getter与Setter方法的内省调用

我们知道在进行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 stub
		
		BBB 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);*/
		
		//方法2
		BeanInfo 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 {
		/*方法1
		PropertyDescriptor p1 = new PropertyDescriptor(name, b.getClass());
		//通过内省方式给age字段赋值 自动调用setAge方法
		Method mathSet = p1.getWriteMethod();
		mathSet.invoke(b, value);
		*/
		
		//方法2
		BeanInfo 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修饰不让找不到方法。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值