Java内省IntroSpector应用

来源:互联网 发布:做java三年攒了50万 编辑:程序博客网 时间:2024/06/05 08:23

内省(IntroSpector)是Java语言对JavaBean 类属性、事件的一种缺省处理方法。
例如类A中有属性name, 那我们可以通过getName,setName 来得到其值或者设置新的值。
通过getName/setName 来访问name属性,这就是默认的规则。
Java中提供了一套API 用来访问某个属性的getter/setter方法,通过这些API 可以使你不需要了解这个规则,这些API存放于包java.beans 中

一般的做法是通过类Introspector的getBeanInfo方法获取某个对象的BeanInfo 信息,然后通过BeanInfo来获取属性的描述器(PropertyDescriptor),通过这个属性描述器就可以获取某个属性对应的getter/setter方法,然后我们就可以通过反射机制来调用这些方法。

我们又通常把javabean的实例对象称之为值对象(Value Object),因为这些bean中通常只有一些信息字段和存储方法,没有功能性方法。

一个JavaBean类可以不当JavaBean用,而当成普通类用。JavaBean实际就是一种规范,当一个类满足这个规范,这个类就能被其它特定的类调用。一个类被当作javaBean使用时,JavaBean的属性是根据方法名推断出来的,它根本看不到java类内部的成员变量。去掉set前缀,然后取剩余部分,如果剩余部分的第二个字母是小写的,则把剩余部分的首字母改成小的。

除了反射用到的类需要引入外,内省需要引入的类如下所示,它们都属于java.beans包中的类,自己写程序的时候也不能忘了引入相应的包或者类。下面代码片断是设置某个JavaBean类某个属性的关键代码:

复制代码
1 package com.ljq.test;
2

3 import java.beans.BeanInfo;
4 import
java.beans.IntrospectionException;
5 import
java.beans.Introspector;
6 import
java.beans.PropertyDescriptor;
7 import
java.lang.reflect.InvocationTargetException;
8 import
java.lang.reflect.Method;
9

10 import org.apache.commons.beanutils.BeanUtils;
11

12
13 publicclass IntrospectorTest {
14

15 publicstaticvoid main(String[] args)throws IllegalArgumentException,
16
        IntrospectionException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
17

18         UserInfo userInfo=new UserInfo("zhangsan","123456");
19         String propertyName="userName"
;
20         Object retVal=
getProperty(userInfo, propertyName);
21         System.out.println("retVal="+retVal);//retVal=zhangsan

22
23         Object value="abc";
24
        setProperty(userInfo, propertyName, value);
25         retVal=
getProperty(userInfo, propertyName);
26         System.out.println("retVal="+retVal);//retVal=abc

27
           //使用BeanUtils工具包操作JavaBean
28         String userName=
BeanUtils.getProperty(userInfo, propertyName);
29         System.out.println("userName="+
userName);
30         BeanUtils.setProperty(userInfo, propertyName,"linjiqin"
);
31         userName=
BeanUtils.getProperty(userInfo, propertyName);
32         System.out.println("userName="+
userName);
33
    }
34

35 /**
36      * 设置属性
37
     *
38      * @param
clazz 对象名
39      * @param
propertyName 属性名
40      * @param
value 属性值
41 */

42 privatestaticvoid setProperty(Object clazz, String propertyName, Object value)
43 throws
IntrospectionException,IllegalAccessException, InvocationTargetException{
44 //方法一

45 /*PropertyDescriptor pd=new PropertyDescriptor(propertyName, clazz.getClass());
46
        Method methodSet=pd.getWriteMethod();
47         methodSet.invoke(clazz, value);*/

48
49 //方法二
50         BeanInfo beanInfo=Introspector.getBeanInfo(clazz.getClass());
51         PropertyDescriptor[] pds=
beanInfo.getPropertyDescriptors();
52 for
(PropertyDescriptor pd:pds){
53 if
(propertyName.equals(pd.getName())){
54                 Method methodSet=
pd.getWriteMethod();
55
                methodSet.invoke(clazz, value);
56 break
;
57
            }
58
        }
59
    }
60

61 /**
62      * 获取属性
63
     *
64      * @param
clazz 对象名
65      * @param
propertyName 属性名
66      * @return

67      * @throws IntrospectionException
68      * @throws
InvocationTargetException
69      * @throws
IllegalAccessException
70      * @throws
IllegalArgumentException
71 */

72 privatestatic Object getProperty(Object clazz, String propertyName)
73 throws
IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
74 //方法一

75 /*PropertyDescriptor pd=new PropertyDescriptor(propertyName, clazz.getClass());
76
        Method methodGet=pd.getReadMethod();
77         return methodGet.invoke(clazz);*/

78
79 //方法二
80         Object retVal=null;
81         BeanInfo beanInfo=
Introspector.getBeanInfo(clazz.getClass());
82         PropertyDescriptor[] pds=
beanInfo.getPropertyDescriptors();
83 for
(PropertyDescriptor pd:pds){
84 if
(propertyName.equals(pd.getName())){
85                 Method methodGet=
pd.getReadMethod();
86                 retVal=
methodGet.invoke(clazz);
87 break
;
88
            }
89
        }
90 return
retVal;
91
    }
92

93 }
复制代码

UserInfo类

复制代码
1 package com.ljq.test;
2

3 publicclass UserInfo {
4 private
String userName;
5 private
String pwd;
6

7 public UserInfo(String userName, String pwd) {
8     super
();
9     this.userName=
userName;
10    this.pwd=
pwd;
11
    }
12

13 public String getUserName() {
14     return
userName;
15
    }
16

17 publicvoid setUserName(String userName) {
18     this.userName=
userName;
19
    }
20

21 public String getPwd() {
22     return
pwd;
23
    }
24

25 publicvoid setPwd(String pwd) {
26     this.pwd=
pwd;
27
    }
28

29 }
复制代码
0 0
原创粉丝点击