内省(Introspector)

来源:互联网 发布:ubuntu matlab 编辑:程序博客网 时间:2024/06/04 20:07

内省(Introspector)是Java语言对JavaBean 类属性、事件的一种缺省处理方法。
   例如类A中有属性name, 那我们可以通过getName,setName 来得到其值或者设置新的值。通过getName/setName 来访问name属性,这就是默认的规则。
   Java中提供了一套API 用来访问某个属性的getter/setter方法,通过这些API 可以使你不需要了解这个规则,这些API存放于包java.beans 中。
   一般的做法是通过类Introspector的getBeanInfo方法获取某个对象的BeanInfo 信息,然后通过BeanInfo来获取属性的描述器(PropertyDescriptor),通过这个属性描述器就可以获取某个属性对应的getter/setter方法,然后我们就可以通过反射机制来调用这些方法。
   我们又通常把JavaBean的实例对象称之为值对象(Value Object),因为这些bean中通常只有一些信息字段和存储方法,没有功能性方法。JavaBean实际是一种规范,当一个类满足这个规范,这个类就能被其它特定的类调用。一个类被当作JavaBean使用时,JavaBean的属性是根据方法名推断出来的,它根本看不到Java类内部的成员变量。去掉set前缀,然后取剩余部分,如果剩余部分的第二个字母是小写的,则把剩余部分的首字母改成小的。

public class Student {    private String name;    private int age;    private Date date;    public Date getDate() {        return date;    }    public void setDate(Date date) {        this.date = date;    }    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;    }}

public class MyPropertyDescriptor {    //内省操作bean方式一    @Test    public void propertyDescriptor() {        Student student = new Student();        try {            PropertyDescriptor propertyDescriptor = new PropertyDescriptor("name",student.getClass());            Method method = propertyDescriptor.getWriteMethod();            method.invoke(student, "张三");            Method method2 = propertyDescriptor.getReadMethod();            String arg = (String) method2.invoke(student, null);            System.out.println(arg);        } catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {            e.printStackTrace();        }    }    //内省操作bean方式二    @Test    public void introspector(){        Student student = new Student();        try {            BeanInfo beanInfo = Introspector.getBeanInfo(student.getClass());            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {                System.out.println(propertyDescriptor.getName());            }        } catch (IntrospectionException e) {            e.printStackTrace();        }    }    //操作bean方式三(加入commons-beanutils.jar,commons-logging.jar)    @Test    public void beanUtils(){        Student student = new Student();        try {            BeanUtils.setProperty(student, "name", "李四");            String property = BeanUtils.getProperty(student, "name");            System.out.println(property);        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {            e.printStackTrace();        }    }    //操作bean方式三(加入commons-beanutils.jar,commons-logging.jar)    @Test    public void beanutils(){        Student student = new Student();        try {            register();            BeanUtils.setProperty(student, "date", "1989-11-07");            String property = BeanUtils.getProperty(student, "date");            System.out.println(property);        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {            e.printStackTrace();        }    }    public void register(){        ConvertUtils.register(new Converter() {            @Override            public Object convert(Class arg0, Object type) {                String arg1 = (String) type;                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");                try {                    return simpleDateFormat.parse(arg1);                } catch (ParseException e) {                    e.printStackTrace();                    return null;                }            }        }, java.util.Date.class);    }    //操作bean方式三(加入commons-beanutils.jar,commons-logging.jar)        @Test        public void converters() throws Exception{            Student s = new Student();            ConvertUtils.register(new DateLocaleConverter(),java.util.Date.class);            BeanUtils.setProperty(s,"date","2011-10-09");            String birthday = BeanUtils.getProperty(s,"date");            System.out.println(birthday);        }}
0 0
原创粉丝点击