“内省(Introspector)是Java 语言对Bean类属性、事件的一种缺省处理方法”,不甚明了。包 java.beans 中提供了一套 API 可以在不知道属性名的情况下用来遍历访问Bean中的属性、某个属性的 getter/setter 方法,以及在反射机制的基础上执行属性的getter/setter方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/** * 一个JavaBean * 1)必须有无参构造函数 2)属性必须私有 3)提供标准的getter和setter * 内省: 通过 反射的方式 访问javabean的技术 */ public class IntrospectorTest { private String theName; private Integer age; public String getTheName() { return theName; } public void setTheName(String name) { theName = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String toString() { return "IntrospectorTest [theName=" + theName + ", age=" + age + "]"; } } |
操作JavaBean示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
public static void main(String[] args) throws Exception { System.err.println("// /////////// 传统操作JavaBean的方式 /////////////////////////////////"); IntrospectorTest demo = new IntrospectorTest(); demo.setTheName("传统方式操作JavaBean"); System.out.println("传统:" + demo.toString()); System.err.println("// /////////// 使用内省操作JavaBean //////////////////////////////////"); // 1.创建JavaBean的实例 IntrospectorTest it = new IntrospectorTest(); // 2.内省器分析JavaBean,得到信息。 // (JavaBean类的运行时文件,如果不想把父类的属性也列出来的话 第二个参数填写父类的信息) BeanInfo bi = Introspector.getBeanInfo(it.getClass(), Object.class); // 3.自动分析得到属性集合 PropertyDescriptor[] pds = bi.getPropertyDescriptors(); // 操作全部属性 for (int i = 0; i < pds.length; i++) { // 4.得到一个属性对象 PropertyDescriptor pd = pds[i]; // 5.得到属性名称 String pdname = pd.getName(); // 6.得到这个属性对应的get方法 Method readMethod = pd.getReadMethod(); System.out.println("readMethod:" + readMethod.getName()); // 7.得到这个属性对应的set方法 Method writeMethod = pd.getWriteMethod(); System.out.println("writeMethod:" + writeMethod.getName()); // 8.执行set方法操作类实例的属性 if (pd.getPropertyType().equals(Integer.class)) { writeMethod.invoke(it, 100); } else { writeMethod.invoke(it, "内省中赋值"); } Object object; // 9.get方法返回的是String(被执行方法的类实例,参数) if (pd.getPropertyType().equals(Integer.class)) { // getPropertyType()得到属性的数据类型 object = (Integer) readMethod.invoke(it, null); } else { object = (String) readMethod.invoke(it, null); } System.out.println(pdname + "=" + object); } System.err.println("// /////////// 使用BeanUtils操作JavaBean ///////////////////////"); // http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi // http://commons.apache.org/proper/commons-logging/download_logging.cgi IntrospectorTest test = new IntrospectorTest(); // 1.BeanUtils的setProperty(类对象,类属性名称,所赋的值)方法为类属性赋值 BeanUtils.setProperty(test, "theName", "使用BU赋值"); BeanUtils.setProperty(test, "age", 88); // 2.BeanUtils的getProperty(object,name)方法的返回值是String类型 String nm = BeanUtils.getProperty(test, "theName"); System.out.println(nm); System.out.println("内省:" + test.toString()); Map map = new HashMap(); map.put("theName", "一次全赋值"); map.put("age", "1234"); // 3.用Map集合中的值填充到Bean的属性 BeanUtils.populate(test, map); System.out.println(test); /* // /////////// 传统操作JavaBean的方式 ///////////////////////////////// 传统:IntrospectorTest [theName=传统方式操作JavaBean, age=null] // /////////// 使用内省操作JavaBean ////////////////////////////////// readMethod:getAge writeMethod:setAge age=100 readMethod:getTheName writeMethod:setTheName theName=内省中赋值 // /////////// 使用BeanUtils操作JavaBean /////////////////////// 使用BU赋值 内省:IntrospectorTest [theName=使用BU赋值, age=88] IntrospectorTest [theName=一次全赋值, age=1234] */ } |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/1346.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设