您的位置:首页 > 编程语言 > Java开发

java--内省

2013-10-24 16:47 465 查看
为什么要学内省?

开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都要使用反射技术完成这样的操作过于麻烦,所以sun公司开发了一套API,用于专门操作java对象的属性

内省访问javaBean的方式:

1、通过PropertyDescriptor类操作Bean的属性

2、通过Introspector类获得Bean对象的BeanInfo,然后,通过BeanInfo来获取属性的描述器PropertyDescriptor,通过这个属性描述器就可以获取某个属性对应的getter/setter方法,然后通过反射机制来调用这些方法

例:

javaBean:Person类

1 /**
2  *
3  * @功能:javaBean
4  *
5  * @日期:2013-10-24
6  *
7  * 关于javaBean的属性,每一个类都会继承Object类,Object类中有一个getClass()属性,
8  * 所以,每个类里的属性里都会有这么一个属性
9  * 如下:Person类里一共是四个属性(getName()或setName(),getAge()或setAge(),getPassword())
10  *         只有get或set方法算是属性,如果没有get或set方法,只有一个字段,并不算是属性,
11  *         且同一个字段的get set只能算一个属性
12  */
13 public class Person {
14
15     private String name;
16     private int age;
17
18     public String getPassword(){
19         return null;
20     }
21
22     public String getName() {
23         return name;
24     }
25     public void setName(String name) {
26         this.name = name;
27     }
28     public int getAge() {
29         return age;
30     }
31     public void setAge(int age) {
32         this.age = age;
33     }
34
35 }


测试方法:

1 import java.beans.BeanInfo;
2 import java.beans.Introspector;
3 import java.beans.PropertyDescriptor;
4 import java.lang.reflect.Method;
5
6 import org.junit.Test;
7
8 /**
9  *
10  * @功能:内省
11  *
12  * @日期:2013-10-24
13  */
14 public class IntropectorTest {
15
16     @Test
17     public void test1() throws Exception{
18         //得到Bean的所有属性
19         BeanInfo allBeanInfo = Introspector.getBeanInfo(Person.class);
20         //得到Person的属性,但不包括其父类Object的属性(即:开始于Person类,止于Object类)
21         BeanInfo beanInfo = Introspector.getBeanInfo(Person.class, Object.class);
22
23         PropertyDescriptor[] pds1 = allBeanInfo.getPropertyDescriptors();
24         PropertyDescriptor[] pds2 = beanInfo.getPropertyDescriptors();
25
26         for (PropertyDescriptor pd1 : pds1) {
27             System.out.println(pd1.getName());
28             //结果:age,class, name, password
29         }
30         for (PropertyDescriptor pd2 : pds2) {
31             System.out.println(pd2.getName());
32             //结果:age, name, password
33         }
34     }
35
36     @Test
37     public void test2() throws Exception{
38         Person p = new Person();
39         //得到name的属性,第一个参数为想要得到的属性的名称,第三个为javaBean的类
40         PropertyDescriptor pd = new PropertyDescriptor("name", Person.class);
41         //得到写的属性
42         Method writeMethod = pd.getWriteMethod();
43         //对属性进行赋值,第一个参数传一个对象进去
44         writeMethod.invoke(p, "张三");
45         //获取读的属性
46         Method readMethod = pd.getReadMethod();
47         //获取属性值并打印(结果:张三)
48         System.out.println(readMethod.invoke(p, null));
49     }
50
51     @Test
52     public void test3() throws Exception{
53         PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);
54         //获取属性并打印(int)
55         System.out.println(pd.getPropertyType());
56     }
57 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: