Java读书笔记十一(Java中的内省机制)

1.前言

最近在学习Java的时候,突然发现了这个概念,鉴于好奇心的压迫,于是打算写一篇博客来总结一下。


 2.什么是内省机制

为了让程序员们更好的从左Java对象的属性,SUN公司开发了一套API,就被我们称为“内省”,有利于我们对类对象的属性的操作,减少了代码的数量。


 3.内省和反射有什么区别

反射式在运行状态把Java类中的各种成分映射成相应的Java类,可以动态的获取所有的属性以及动态调用任意一个方法,强调的是运行状态。

内省机制是通过反射来实现的,BeanInfo用来暴露一个bean的属性、方法和事件,以后我们就可以操纵该JavaBean的属性


 4.一张图总结



在Java内省中,用到的基本上就是上述几个类。

通过BeanInfo这个类就可以获取到类中的方法和属性,具体的可以参考JDK文档


 5.Demo展示

5.1.通过PropertyDescriptor修改属性方式

[java]  view plain  copy
  1. package com.query;  
  2.   
  3. import java.beans.PropertyDescriptor;  
  4. import java.lang.reflect.Method;  
  5.   
  6. public class BeanInfoUtil {  
  7.     public static void setProperty(UserInfo userInfo,String userName)throws Exception{  
  8.         PropertyDescriptor propDesc=new PropertyDescriptor(userName,UserInfo.class);  
  9.         Method methodSetUserName=propDesc.getWriteMethod();  
  10.         methodSetUserName.invoke(userInfo, "wong");  
  11.         System.out.println("set userName:"+userInfo.getUserName());  
  12.     }  
  13.     
  14.     public static void getProperty(UserInfo userInfo,String userName)throws Exception{  
  15.         PropertyDescriptor proDescriptor =new PropertyDescriptor(userName,UserInfo.class);  
  16.         Method methodGetUserName=proDescriptor.getReadMethod();  
  17.         Object objUserName=methodGetUserName.invoke(userInfo);  
  18.         System.out.println("get userName:"+objUserName.toString());  
  19.     }  
  20.   
  21. }  

5.2.通过Introspector类修改属性

[java]  view plain  copy
  1. package com.query;  
  2.   
  3. import java.beans.BeanInfo;  
  4. import java.beans.Introspector;  
  5. import java.beans.PropertyDescriptor;  
  6. import java.lang.reflect.Method;  
  7.   
  8. public class BeanInfoUtil2 {  
  9.     public static void setPropertyByIntrospector(UserInfo userInfo,  
  10.             String userName) throws Exception {  
  11.           
  12.         BeanInfo beanInfo = Introspector.getBeanInfo(UserInfo.class);  
  13.         PropertyDescriptor[] proDescrtptors = beanInfo.getPropertyDescriptors();  
  14.         if (proDescrtptors != null && proDescrtptors.length > 0) {  
  15.             for (PropertyDescriptor propDesc : proDescrtptors) {  
  16.                 if (propDesc.getName().equals(userName)) {  
  17.                     Method methodSetUserName = propDesc.getWriteMethod();  
  18.                     methodSetUserName.invoke(userInfo, "alan");  
  19.                     System.out  
  20.                             .println("set userName:" + userInfo.getUserName());  
  21.                     break;  
  22.                 }  
  23.             }  
  24.         }  
  25.     }  
  26.   
  27.     public static void getPropertyByIntrospector(UserInfo userInfo,  
  28.             String userName) throws Exception {  
  29.         BeanInfo beanInfo = Introspector.getBeanInfo(UserInfo.class);  
  30.         PropertyDescriptor[] proDescrtptors = beanInfo.getPropertyDescriptors();  
  31.         if (proDescrtptors != null && proDescrtptors.length > 0) {  
  32.             for (PropertyDescriptor propDesc : proDescrtptors) {  
  33.                 if (propDesc.getName().equals(userName)) {  
  34.                     Method methodGetUserName = propDesc.getReadMethod();  
  35.                     Object objUserName = methodGetUserName.invoke(userInfo);  
  36.                     System.out  
  37.                             .println("get userName:" + objUserName.toString());  
  38.                     break;  
  39.                 }  
  40.             }  
  41.         }  
  42.     }  
  43.   
  44. }  


注意事项,在上述修改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/hotpots/article/details/80655982
今日推荐