在這裏我們首先要創建一個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;
}
}