内省的解释
内省在 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/
来源: http://www.bubuko.com/infodetail-2525684.html