在这里我们首先要创建一个JavaBean类
先让我们了解什么是JavaBean吧:
JavaBean 是一种JAVA语言写成的可重用组件。为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器。JavaBean 通过提供符合一致性设计模式的公共方法将内部域暴露成员属性。众所周知,属性名称符合这种模式,其他Java 类可以通过自身机制发现和操作这些JavaBean 属性。
下面是一个标准的JavaBean类
import java.util.Date;
public class ReflectionPoint {
private int x;
private int y;
private Date time = new Date();
public ReflectionPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
public ReflectionPoint() {
super();
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
下面我们通过内省进行参数的取值以及设值,代码如下
在这里我们需要用到两个jar包,可以自己百度或者到apache官网下载
1.commons-beanutils-1.8.3-bin
2.commons-logging-1.1.1-bin
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class Introspection {
// javaBean的内省机制
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ReflectionPoint rp = new ReflectionPoint(3, 5);
// 取值
String propertyName = "x";
Object retVal = getProperty(rp, propertyName);//此方法已经抽取到西面
System.out.println(retVal);
// 设值
Object value = 7;
PropertyDescriptor pd = setProperty(rp, propertyName, value);//此方法已经抽取到下面
Object retVal2 = getProperty(rp, propertyName);
System.out.println(retVal2);
/**
* 在这里我们采用第三方的BeanUtils工具
* 对JavaBean进行取值
* 使用第三方提供的jar可以简化我们的编程,方便很多
* 关于前面介绍的自己写的方法主要是向大家介绍
* 什么事JavaBean如何炒作javaBean类
*/
Object x = BeanUtils.getProperty(rp, "x");
System.out.println(x);
BeanUtils.setProperty(rp, "time.time", "111");
Object time = BeanUtils.getProperty(rp, "time.time");
System.out.println(time);
//java7新特性中
//Map map = {name:"zxx",age:20};
//BeanUtils.setProperty(map, "name", "fldy");
//安本来类型对参数进行操作,当您不想进行类型转换时,可以用下面的方法
PropertyUtils.setProperty(rp, "y", 9);
System.out.println(PropertyUtils.getProperty(rp, "y").getClass());
}
//通过某个方法设置某个类的某个属性的值
private static PropertyDescriptor setProperty(Object rp,
String propertyName, Object value) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName, rp
.getClass());
Method methodSetX = pd.getWriteMethod();
methodSetX.invoke(rp, value);
return pd;
}
//通过某个方法取得某个类的某个属性的值
private static Object getProperty(Object rp, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
/*PropertyDescriptor pd = new PropertyDescriptor(propertyName, rp
.getClass());
Method methodGetX = pd.getReadMethod();
Object retVal = methodGetX.invoke(rp);*/
BeanInfo beanInfo = Introspector.getBeanInfo(rp.getClass());//首先获取一个类的BeanInfo信息
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();//由于通过BeanInfo我们只能得到所有的方法
//不能得到某个方法所以下面我们还必须循环判断并取得我们需要的方法
Object retVal = null;
for(PropertyDescriptor pd: pds){
if(pd.getName().equals(propertyName)){
Method methodGetX = pd.getReadMethod();
retVal = methodGetX.invoke(rp);
}
}
return retVal;
}
}