Introspector----》javabean---》特殊的java类
VO,,,value object 值对象,,,
PropertyDescripor属性详情,可以通过它来的getReadMethod和getWriteMethod来获得该属性的的getter和setter方法
Introspector的一个静态方法getBeanInfo返回一个BeanInfo类型的对象;
BeanUtils有很多静态方法,比如setProperty和getProperty方法,可以直接对bean对象进行操作,,而且支持级联属性设置。
比如给Student加一个生日字段,类型为date,,,我们就可以通过setProperty(stu1,"birthday.time","111")对它进行操作。,。
以及将bean中的属性通过调用descibe方法封装到一个map对象当中。。
package javabean; import java.util.Date; public class Student { public Student() { } public Student(String name, int age, String sex) { this.name = name; this.age = age; this.sex = sex; } private Date birthday = new Date(); public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } private String name; private int age; private String sex; 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String toString(){ return this.name+":"+this.age+":"+this.sex+":"+this.birthday; } } package javabean; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.PropertyUtils; public class IntrospectorTest { public static void main(String[] args) throws Exception { Constructor con = Class.forName("javabean.Student").getConstructor( String.class,int.class,String.class); Object obj = con.newInstance("zhangsan",20,"male"); System.out.println(((Student) obj)); String propertyName = "age"; PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass()); Method methodSetAge = pd.getWriteMethod(); methodSetAge.invoke(obj, 99); System.out.println(((Student) obj)); Method methodGetAge = pd.getReadMethod(); Object retVal = methodGetAge.invoke(obj); System.out.println(retVal); Method methodToString = obj.getClass().getMethod("toString"); System.out.println(methodToString.invoke(obj)); BeanInfo bi = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] props = bi.getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : props) { if(propertyDescriptor.getName().equals(propertyName)){ Method meGetAge = propertyDescriptor.getReadMethod(); System.out.println(meGetAge.invoke(obj)); break; } } System.out.println(BeanUtils.getProperty(obj, "age")); BeanUtils.setProperty(obj, "name", "lisi"); System.out.println(obj); BeanUtils.setProperty(obj, "birthday.time", "11"); System.out.println(obj); PropertyUtils.setProperty(obj, "sex", "female"); PropertyUtils.setProperty(obj, "age", 1); System.out.println(PropertyUtils.getProperty(obj, "sex")); System.out.println(obj+obj.getClass().getName()); Map map = BeanUtils.describe(obj); for (Object val : map.values()) { System.out.println(val); } Student stu2 = new Student("zhangsan",22,"male"); Map map2 = new HashMap(); BeanUtils.populate(stu2, map2); System.out.println(map2.size()); for (Object val : map2.values()) { System.out.println(val); } } }
希望不用通过自己类型转换就能对bean对象进行操作,可以使用bealUtils类中的静态方,,
如果需要自己手动进行类型转换就可以使用PropertyUtils类中的静态方法,,,可实现与bealutils类一样的效果!!!