一、关于javabean
首先,大家需要明白什么是javabean。在以前我的想法中,只要是.java结尾的文件就是javabean,这种理解是错误的。
必须满足如下条件:
1、属性是私有的
2、提供标准的gettter()和setter()方法
3、必须有无参数的构造函数
二、关于javabean的内省操作
内省定义:通过反射的方式访问javabean中的属性。
用到的jdk中的类:PropertyDescriptor
1、对javabean的简单内省操作:
首先,我们先定义一个简单的javabean:
package com.reflect;
public class ReflectPoint {
private int x;
private int y;
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
接下来,我们需要编写一个test测试
package com.reflect;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class IntroSpectorTest{
public static void main(String[] args) throws Exception {
ReflectPoint pt1 = new ReflectPoint(3,5);
String propertyName = "x"; //定义javabean中的属性名
Object retVal = getProperty(pt1, propertyName);
System.out.println(retVal); //3
Object value = 7;
setProperties(pt1, propertyName, value);
System.out.println(pt1.getX());//7
}
private static void setProperties(Object pt1, String propertyName,
Object value) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
//创建属性描述器
PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());
//获得写方法
Method methodSetX = pd.getWriteMethod();
methodSetX.invoke(pt1, value);
}
private static Object getProperty(Object pt1, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());
Method methodGetX = pd.getReadMethod(); //得到了x属性的读方法
Object retVal = methodGetX.invoke(pt1);
return retVal;
}
}
2、对javabean的复杂内省操作:
通过类 Introspector 来获取某个对象的 BeanInfo 信息,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后我们就可以通过反射机制来调用这些方法。
还是先创建一个javabean,同上:
package com.reflect;
public class ReflectPoint {
private int x;
private int y;
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
然后,编写一个test测试类:
package com.reflect;
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;
public class IntroSpectorTest{
public static void main(String[] args) throws Exception {
ReflectPoint pt1 = new ReflectPoint(3,5);
String propertyName = "x"; //定义javabean中的属性名
Object retVal = getProperty(pt1, propertyName);
System.out.println(retVal);
Object value = 7;
setProperties(pt1, propertyName, value);
System.out.println(pt1.getX());
}
private static void setProperties(Object pt1, String propertyName,
Object value) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor pd : pds){
if(pd.getName().equals(propertyName)){
Method methodSetX = pd.getWriteMethod();
methodSetX.invoke(pt1, value);
break;
}
}
}
private static Object getProperty(Object pt1, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
Object retVal = null;
for(PropertyDescriptor pd : pds){
if(pd.getName().equals(propertyName)){
Method methodGetX = pd.getReadMethod();
retVal = methodGetX.invoke(pt1);
break;
}
}
return retVal;
}
}
输出的结果和简单内省的相同、
原文:http://blog.csdn.net/u010800530/article/details/38827829