Java Introspector(内省)

来源:互联网 发布:广州网络推广gzdlwl 编辑:程序博客网 时间:2024/06/05 08:23

POJO

public class Student {    // 创建属性(含有 get|set 的Field)    private String name;    private int age;    private Date birthday;    public Date getBirthday() {        return birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {  //age        return age;    }    public void setAge(int age) {        this.age = age;    }    // 只有 set    public int getAbc(){  //abc        return 10;    }}

内省

  1. Introspector构建一个BeanInfo对象。
  2. BeanInfo将一个类的所有属性进行了封装
  3. 获取属性描述器
    PropertyDescriptor[] pds = bf.getPropertyDescriptors();
  4. PropertyDescriptor对象:
    1. Method getReadMethod()
    获得应该用于读取属性值的方法。
    2. Method getWriteMethod()
    获得应该用于写入属性值的方法。
public class 内省 {    @Test    public void test1() throws Exception {        // 将Student中的所有属性封装到BeanInfo对象中        BeanInfo bf = Introspector.getBeanInfo(Student.class);        // 属性描述器        PropertyDescriptor[] pds = bf.getPropertyDescriptors();        // System.out.println(pds.length);        for (PropertyDescriptor pd : pds) {            System.out.println(pd.getName());        }        /*        5        abc        age        birthday        class        name        */    }    @Test    public void test2() throws Exception {        Student stu = new Student();        // 得到指定的属性对象        PropertyDescriptor pd = new PropertyDescriptor("name", Student.class);        Method setter = pd.getWriteMethod();// 得到setter方法:setName()        setter.invoke(stu, "tom");        Method getter = pd.getReadMethod(); // 得到getter方法:getName()        System.out.println(getter.invoke(stu, null));    }    @Test    public void test3() throws Exception {        Student stu = new Student();        // 得到指定的属性对象        PropertyDescriptor pd = new PropertyDescriptor("age", Student.class);        Method setter = pd.getWriteMethod();// 得到setter方法:setName()        setter.invoke(stu, 18);        Method getter = pd.getReadMethod(); // 得到getter方法:getName()        System.out.println(getter.invoke(stu, null));    }}

BeanUtils 的封装使用

public class BeanUtil {    @Test    public void test1() throws Exception{        Student stu = new Student();        BeanUtils.setProperty(stu, "name", "张三"); //给属性赋值        String s = BeanUtils.getProperty(stu, "name");        System.out.println(s);    }    @Test    public void test2() throws Exception{        Student stu = new Student();        //BeanUtils默认支持8种基本数据类型,自动转换        BeanUtils.setProperty(stu, "age", "18"); //给属性赋值        String s = BeanUtils.getProperty(stu, "age");        System.out.println(s);    }    @Test    public void test3() throws Exception{        Student stu = new Student();        //注册类型转换器        ConvertUtils.register(new DateLocaleConverter(), Date.class);        BeanUtils.setProperty(stu, "birthday", "1990-11-12"); //给属性赋值        String s = BeanUtils.getProperty(stu, "birthday");        System.out.println(s);    }    @Test    public void test4() throws Exception{        Student stu = new Student();        //注册类型转换器        ConvertUtils.register(new Converter() {            public Object convert(Class type, Object value) {                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");                if(value instanceof String){                    String v = (String) value;                    try {                        return sdf.parse(v);                    } catch (ParseException e) {                        throw new RuntimeException(e);                    }                }                return null;            }        }, Date.class);        BeanUtils.setProperty(stu, "birthday", "1995-11-12"); //给属性赋值        String s = BeanUtils.getProperty(stu, "birthday");        System.out.println(s);    }    @Test    public void test5() throws Exception{        //        Map m = new HashMap();        m.put("name", "张三"); //key名一定要与对象中的变量名一致        m.put("age", "18"); //key名一定要与对象中的变量名一致        m.put("birthday", "1992-05-12"); //key名一定要与对象中的变量名一致        Student stu = new Student();        ConvertUtils.register(new DateLocaleConverter(), Date.class);        BeanUtils.populate(stu, m); //将Map属性自动放到Bean中        System.out.println(stu.getName());    }}