该博客主要参考了一下博客而作:
(1) http://www.cnblogs.com/linjiqin/archive/2011/02/16/1956266.html
(2) http://orange5458.iteye.com/blog/1047685
IntroSpecor介绍
内省(IntroSpector)是Java语言对JavaBean 类属性、事件的一种缺省处理方法。
例如类A中有属性name, 那我们可以通过getName,setName 来得到其值或者设置新的值。
通过getName/setName 来访问name属性,这就是默认的规则。
Java中提供了一套API 用来访问某个属性的getter/setter方法,通过这些API 可以使你不需要了解这个规则,这些API存放于包java.beans 中。
Class Diagram
一般的做法是通过类Introspector的getBeanInfo方法获取某个对象的BeanInfo 信息,然后通过BeanInfo来获取属性的描述器(PropertyDescriptor),通过这个属性描述器就可以获取某个属性对应的getter/setter方法,然后我们就可以通过反射机制来调用这些方法。
我们又通常把javabean的实例对象称之为值对象(Value Object),因为这些bean中通常只有一些信息字段和存储方法,没有功能性方法。
一个JavaBean类可以不当JavaBean用,而当成普通类用。JavaBean实际就是一种规范,当一个类满足这个规范,这个类就能被其它特定的类调用。一个类被当作javaBean使用时,JavaBean的属性是根据方法名推断出来的,它根本看不到java类内部的成员变量。去掉set前缀,然后取剩余部分,如果剩余部分的第二个字母是小写的,则把剩余部分的首字母改成小的。
除了反射用到的类需要引入外,内省需要引入的类如下所示,它们都属于java.beans包中的类,自己写程序的时候也不能忘了引入相应的包或者类。
简单示例1
下面代码片断是设置某个JavaBean类某个属性的关键代码:
package com.jasun.test;
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;
publicclass IntrospectorTest {
publi static void main(String[] args) throws IllegalArgumentException,
IntrospectionException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
UserInfo userInfo=new UserInfo("zhangsan", "123456");
String propertyName="userName";
Object retVal=getProperty(userInfo, propertyName);
System.out.println("retVal="+retVal); //retVal=zhangsan
Object value="abc";
setProperty(userInfo, propertyName, value);
retVal=getProperty(userInfo, propertyName);
System.out.println("retVal="+retVal); //retVal=abc
//使用BeanUtils工具包操作JavaBean
String userName=BeanUtils.getProperty(userInfo, propertyName);
System.out.println("userName="+userName);
BeanUtils.setProperty(userInfo, propertyName, "linjiqin");
userName=BeanUtils.getProperty(userInfo, propertyName);
System.out.println("userName="+userName);
}
/**
* 设置属性
*
* @param clazz 对象名
* @param propertyName 属性名
* @param value 属性值
*/
private static void setProperty(Object clazz, String propertyName, Object value)
throws IntrospectionException,IllegalAccessException, InvocationTargetException{
//方法一
/*PropertyDescriptor pd=new PropertyDescriptor(propertyName, clazz.getClass());
Method methodSet=pd.getWriteMethod();
methodSet.invoke(clazz, value);*/
//方法二
BeanInfo beanInfo=Introspector.getBeanInfo(clazz.getClass());
PropertyDescriptor[] pds=beanInfo.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
if(propertyName.equals(pd.getName())){
Method methodSet=pd.getWriteMethod();
methodSet.invoke(clazz, value);
break;
}
}
}
/**
* 获取属性
*
* @param clazz 对象名
* @param propertyName 属性名
* @return
* @throws IntrospectionException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
private static Object getProperty(Object clazz, String propertyName)
throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
//方法一
/*PropertyDescriptor pd=new PropertyDescriptor(propertyName, clazz.getClass());
Method methodGet=pd.getReadMethod();
return methodGet.invoke(clazz);*/
//方法二
Object retVal=null;
BeanInfo beanInfo=Introspector.getBeanInfo(clazz.getClass());
PropertyDescriptor[] pds=beanInfo.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
if(propertyName.equals(pd.getName())){
Method methodGet=pd.getReadMethod();
retVal=methodGet.invoke(clazz);
break;
}
}
return retVal;
}
}
UserInfo类
package com.ljq.test;
publicclass UserInfo {
private String userName;
private String pwd;
public UserInfo(String userName, String pwd) {
super();
this.userName = userName;
this.pwd = pwd;
}
public String getUserName() {
return userName;
}
publicvoid setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
publicvoid setPwd(String pwd) {
this.pwd = pwd;
}
}
简单示例2::(仅作参考)
package com.siyuan.jdktest;
import java.beans.BeanDescriptor;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
class Person {
private String name;
private int age;
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
public class IntrospectorTest {
/**
* @param args
* @throws IntrospectionException
*/
public static void main(String[] args) throws IntrospectionException {
// TODO Auto-generated method stub
BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
System.out.println("BeanDescriptor===========================================");
BeanDescriptor beanDesc = beanInfo.getBeanDescriptor();
Class cls = beanDesc.getBeanClass();
System.out.println(cls.getName());
System.out.println("MethodDescriptor===========================================");
MethodDescriptor[] methodDescs = beanInfo.getMethodDescriptors();
for (int i = 0; i < methodDescs.length; i++) {
Method method = methodDescs[i].getMethod();
System.out.println(method.getName());
}
System.out.println("PropertyDescriptor===========================================");
PropertyDescriptor[] propDescs = beanInfo.getPropertyDescriptors();
for (int i = 0; i < propDescs.length; i++) {
Method methodR = propDescs[i].getReadMethod();
if (methodR != null) {
System.out.println(methodR.getName());
}
Method methodW = propDescs[i].getWriteMethod();
if (methodW != null) {
System.out.println(methodW.getName());
}
}
}
}
3. 运行结果
BeanDescriptor===========================================
com.siyuan.jdktest.Person
MethodDescriptor===========================================
hashCode
setAge
equals
wait
wait
notify
getClass
toString
getAge
notifyAll
setName
wait
getName
PropertyDescriptor===========================================
getAge
setAge
getClass
getName
setName