Java - Java内省详解


内省的解释

内省在wiki上的解释:

在计算机科学中,内省是指计算机程序在运行时(Run time)检查对象(Object)类型的一种能力,通常也可以称作运行时类型检查。 
不应该将内省和反射混淆。相对于内省,反射更进一步,是指计算机程序在运行时(Run time)可以访问、检测和修改它本身状态或行为的一种能力。


内省和反射有什么区别

反射式在运行状态把Java类中的各种成分映射成相应的Java类,可以动态的获取所有的属性以及动态调用任意一个方法,强调的是运行状态。 
内省机制是通过反射来实现的,BeanInfo用来暴露一个bean的属性、方法和事件,以后我们就可以操纵该JavaBean的属性


这里写图片描述
在Java内省中,用到的基本上就是上述几个类。 
通过BeanInfo这个类就可以获取到类中的方法和属性,具体的可以参考JDK文档


内省的代码实现

1.通过PropertyDescriptor修改属性方式

[java] view plain copy print?
package com.query;  

import java.beans.PropertyDescriptor;  
import java.lang.reflect.Method;  

public class BeanInfoUtil {  
    public static void setProperty(UserInfo userInfo,String userName)throws Exception{  
        PropertyDescriptor propDesc=new PropertyDescriptor(userName,UserInfo.class);  
        Method methodSetUserName=propDesc.getWriteMethod();  
        methodSetUserName.invoke(userInfo, "wong");  
        System.out.println("set userName:"+userInfo.getUserName());  
    }  

    public static void getProperty(UserInfo userInfo,String userName)throws Exception{  
        PropertyDescriptor proDescriptor =new PropertyDescriptor(userName,UserInfo.class);  
        Method methodGetUserName=proDescriptor.getReadMethod();  
        Object objUserName=methodGetUserName.invoke(userInfo);  
        System.out.println("get userName:"+objUserName.toString());  
    }  

}  

2.通过Introspector类修改属性

package com.query;  

import java.beans.BeanInfo;  
import java.beans.Introspector;  
import java.beans.PropertyDescriptor;  
import java.lang.reflect.Method;  

public class BeanInfoUtil2 {  
    public static void setPropertyByIntrospector(UserInfo userInfo,  
            String userName) throws Exception {  

        BeanInfo beanInfo = Introspector.getBeanInfo(UserInfo.class);  
        PropertyDescriptor[] proDescrtptors = beanInfo.getPropertyDescriptors();  
        if (proDescrtptors != null && proDescrtptors.length > 0) {  
            for (PropertyDescriptor propDesc : proDescrtptors) {  
                if (propDesc.getName().equals(userName)) {  
                    Method methodSetUserName = propDesc.getWriteMethod();  
                    methodSetUserName.invoke(userInfo, "alan");  
                    System.out  
                            .println("set userName:" + userInfo.getUserName());  
                    break;  
                }  
            }  
        }  
    }  

    public static void getPropertyByIntrospector(UserInfo userInfo,  
            String userName) throws Exception {  
        BeanInfo beanInfo = Introspector.getBeanInfo(UserInfo.class);  
        PropertyDescriptor[] proDescrtptors = beanInfo.getPropertyDescriptors();  
        if (proDescrtptors != null && proDescrtptors.length > 0) {  
            for (PropertyDescriptor propDesc : proDescrtptors) {  
                if (propDesc.getName().equals(userName)) {  
                    Method methodGetUserName = propDesc.getReadMethod();  
                    Object objUserName = methodGetUserName.invoke(userInfo);  
                    System.out  
                            .println("get userName:" + objUserName.toString());  
                    break;  
                }  
            }  
        }  
    }  

}  

注意事项,在上述修改JavaBean属性的时候,如果数据类型不对的话,会报错。例如BeanInfoUtil.setProperty(userInfo, “age”);报错是应为age属性是int数据类型,而setProperty方法里面默认给age属性赋的值是String类型。所以会爆出argument type mismatch参数类型不匹配的错误信息。 
为了解决上述问题,Apache开发了一套简单、易用的API来操作Bean的属性——BeanUtils工具包。 
  BeanUtils工具包:下载:http://commons.apache.org/beanutils/ 注意:应用的时候还需要一个logging包http://commons.apache.org/logging/


猜你喜欢

转载自blog.csdn.net/qq_40395278/article/details/80414068