使用Java的Introspector类操作JavaBean的属性
来源:互联网 发布:淘宝店推广策划报告 编辑:程序博客网 时间:2024/06/03 17:05
Introspector类是Java提供的操作JavaBean的属性的工具类
Introspector类操作JavaBean属性的示例:
JavaBean:Person
package com.test.introspector;/** * Person * @author xuhu * */public class Person{ //id private int id; //姓名 private String name; //年龄 private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
package com.test.introspector;import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import org.junit.Test;/** * 内省测试类 * @author xuhu * */public class IntrospectorTest{ private Person p = new Person(); /** * 获取Person类所有属性 * @throws IntrospectionException */ @Test public void test1() throws IntrospectionException { BeanInfo beanInfo = Introspector.getBeanInfo(Person.class); PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); //输出Person类所有属性 for(PropertyDescriptor pd : pds) { System.out.println(pd.getName()); } } /** * Person类属性赋值 方法1 * @throws IntrospectionException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws Exception */ @Test public void test2() throws Exception { BeanInfo beanInfo = Introspector.getBeanInfo(Person.class); PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); //创建Person对象 for(PropertyDescriptor pd : pds) { if("name".equals(pd.getName())) { Method method = pd.getWriteMethod(); method.invoke(p, "测试用户"); } } System.out.println(p.getName()); } /** * Person类属性赋值 方法2 * @throws Exception */ @Test public void test3() throws Exception { PropertyDescriptor pd = new PropertyDescriptor("name", Person.class); Method method = pd.getWriteMethod(); method.invoke(p, "测试用户2"); System.out.println(p.getName()); } /** * Person类属性读取方法 * @throws Exception */ @Test public void test4() throws Exception { PropertyDescriptor pd = new PropertyDescriptor("name", Person.class); Method method = pd.getWriteMethod(); method.invoke(p, "测试用户2"); method = pd.getReadMethod(); String str = String.valueOf(method.invoke(p, null)); System.out.println(str + "#"); }}
0 0
- 使用Java的Introspector类操作JavaBean的属性
- 内省(Introspector)操作JavaBean的属性
- 内省(Introspector)操作JavaBean的属性
- 内省(Introspector)操作JavaBean的属性
- java基础之内省(Introspector)操作javabean属性的理解(主要用于开发框架)
- java-javabean Introspector的应用
- 【Java进阶】内省IntroSpector操作JavaBean和Apache-commons-dbutils对内省的使用
- 类Introspector的使用
- 类Introspector的使用
- 通过PropertyDescriptor和Introspector对JavaBean的简单内省操作
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- hdu-oj 2001 计算两点间距离
- Reward——前向星,拓扑排序
- HDU_1230 火星A+B
- 笔记
- 【算法小总结】广度优先搜索剖析
- 使用Java的Introspector类操作JavaBean的属性
- windows关于程序release 加上调试信息 利用pdb dump定位的问题
- JAVA垃圾回收机制
- hibernate中的inverse和cascade
- hdu-oj 2003 求绝对值
- MyEclipse调试总结
- hdu 2068
- mysql存储过程详解
- hdu-oj 2004 成绩转换