1. 内省的作用 :
开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都使用反射技术完成此类操作过于麻烦,所以sun公司开发了一套API,专门用于操作java对象的属性。
内省的过程就是访问javabean的过程。有时候并不知道javabean中的代码,比如做框架开发时,这时候就要用到内省了。
l 内省访问JavaBean属性的两种方式:
• 通过PropertyDescriptor类操作Bean的属性
• 通过Introspector类获得Bean对象的 BeanInfo,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的getter/setter 方法,然后通过反射机制来调用这些方法。
2. 了解内省
内省是 Java 语言对 Bean 类属性 的一种缺省处理方法。例如类 A 中有属性name, 可以通过 getName,setName 来得到其值或者设置新的值。通过 getName/setName 来访问 name 属性,这是默认的规则。 Java 中提供了一套 API 来访问某个属性的 getter/setter 方法。 一般的做法是通过类 Introspector 来获取某个对象的 BeanInfo 信息,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后通过反射机制来调用这些方法
3. 案例分析
package com.java.Bean;
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 BeanDemo1 {
/**
* @param args
* @throws IntrospectionException
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void main(String[] args) throws IntrospectionException,IllegalAccessException, IllegalArgumentException, InvocationTargetException {
test4 ();
}
// 通过内省获取 person bean 的所有属性
public static void test1() throws IntrospectionException {
BeanInfo bi = Introspector. getBeanInfo (Person. class , Object. class ); /* Introspector 类为通过工具学习有关受目标 Java Bean 支持的属性、事件和方法的知识提供了一个标准方法。
BeanInfor 类实现此 BeanInfo 接口并提供有关其 bean 的方法、属性、事件等显式信息;
方法后面的参数 Object.class 是向上终止类,不不然 getBeanInfo 方法会一直向上索引,这里不加这个参数,输出结果会向上调用父类 Object 中的 getClass*/
PropertyDescriptor[] pd =bi.getPropertyDescriptors();
for (PropertyDescriptor pds : pd) {
System. out .println(pds.getName());
}
}
// 通过内省给 person 的 name 属性赋值: qwe setName(" abc ")
public static void test2() throws IntrospectionException, IllegalAccessException,IllegalArgumentException, InvocationTargetException {
Person p = new Person();
BeanInfo bi = Introspector. getBeanInfo (Person. class );
PropertyDescriptor[] pd =bi.getPropertyDescriptors();
for (PropertyDescriptor pds : pd) {
String name = pds.getName();
if (name.equals( "name" )) { // 这里两个 name 不同,一个是 for 循环里声明的,一个是 javaBean 中的,后者即 equals ()方法中的一定要加上引号
Method m =pds.getWriteMethod(); // 返回一个 reflect (反射)类 Method
m.invoke(p, "qwe" ); // 对带有指定参数的指定对象调用由此 Method 对象表示的底层方法。
}
}
System. out .println(p.getName());
}
// 通过 PropertyDescriptor 类操作 Bean 的属性 name 属性赋值: abc setName(" abc ")
public static void test3() throws IntrospectionException,IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Person p = new Person();
PropertyDescriptor pd = new PropertyDescriptor( "name" ,p.getClass()); //PropertyDescriptor 描述 Java Bean 通过一对存储器方法导出的一个属性。
Method m = pd.getWriteMethod(); // 获得应该用于读取属性值的方法。相当于 javabean 中的 get 方法
m.invoke(p, "abc" );
System. out .println(p.getName());
}
public static void test4() throws IntrospectionException,IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Person p = new Person();
p.setName( " 汉斯 " );
PropertyDescriptor pd = new PropertyDescriptor( "name" ,p.getClass());
Method m = pd.getReadMethod(); // 获得应该用于读取属性值的方法。相当于 set 方法
String str = (String) m.invoke(p, null ) ;
System. out .println(str);
}
}