内省(Introspector)

来源:互联网 发布:国乒惨败 知乎 编辑:程序博客网 时间:2024/06/15 21:14

1.为什么要学内省?

开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都使用反射技术完成此类操作过于麻烦,所以sun公司开发了一套API,专门用于操作java对象的属性。

2.什么是Java对象的属性和属性的读写方法?

内省访问JavaBean属性的两种方式:

通过PropertyDescriptor类操作Bean的属性

通过Introspector类获得Bean对象的 BeanInfo,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后通过反射机制来调用这些方法。

3.内省—beanutils工具包

Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单、易用的API操作Bean的属性——BeanUtils

Beanutils工具包的常用类:

BeanUtils

PropertyUtils

ConvertUtils.regsiter(Converter convert, Class clazz)

自定义转换器

public void test() throws Exception{

Student stu=new Student();

BeanInfo entity=Introspector.getBeanInfo(Student.class);

PropertyDescriptor pds[]= entity.getPropertyDescriptors();

for(PropertyDescriptor pd:pds){

System.out.println(pd.getName());

System.out.println(pd.getShortDescription());

System.out.println(pd.getDisplayName());

if(pd.equals("age")){

Method md=pd.getWriteMethod();

md.invoke(stu, 122);

}

}

System.out.println(stu.getAge());

}

    通过Introspector类获得Bean对象的 BeanInfo,然后通过 BeanInfo 来获取属性的描述器(PropertyDescriptor ) 通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,

然后通过反射机制来调用这些方法。

@Test

public void test() throws Exception {

Student st = new Student();

// 1、通过Introspector类获得Bean对象的 BeanInfo,

BeanInfo entity = Introspector.getBeanInfo(Student.class);

// 2、然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor )

PropertyDescriptor pdrs[] = entity.getPropertyDescriptors();

// 3、通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,

for (PropertyDescriptor pd : pdrs) {

System.out.println(pd.getName());

    System.out.println(pd.getShortDescription());

        System.out.println(pd.getDisplayName());

    if (pd.getName().equals("age")) {  //age是什么类型?

Method md = pd.getWriteMethod();

md.invoke(st, 12);

}

//获取属性的类型 System.out.println(pd.getName()+""+pd.getPropertyType()); 

}

System.out.println(st.getAge());

}

//简便的方法

@Test

public void test1()throws Exception{

Student st = new Student();

//通过构造器 创建 PropertyDescriptor对象

PropertyDescriptor pd = new PropertyDescriptor("age",  Student.class);

Method md = pd.getWriteMethod(); //写操作

md.invoke(st, 120);

System.out.println(st.getAge());

md = pd.getReadMethod();

int value = (Integer)md.invoke(st, null); //读操作

System.out.println(value);

}

4、实例

public class Demo01 {

@Test

public void test1()throws Exception{

Class cls = Class.forName("cn.csdn.beanutils.Student");

Student bean  = (Student)cls.newInstance();

BeanUtils.setProperty(bean, "name", "haiyan");

String name=BeanUtils.getProperty(bean, "name");

System.out.println(name);

System.out.println(bean.getName());

}

@Test

public void test2()throws Exception{

String str="cn.csdn.beanutils.Student";

String age="age";

Class cls = Class.forName(str);

Student bean  = (Student)cls.newInstance();

BeanUtils.setProperty(bean, age, "100");

String a=BeanUtils.getProperty(bean, age);

System.out.println(a);

System.out.println(bean.getAge());

}

@Test

public void test3() throws Exception {

Student bean = new Student();

BeanUtils.setProperty(bean, "brithday", new Date());

System.out.println(bean.getBrithday());

}

@Test

public void test4() throws Exception{

Student bean=new Student();

ConvertUtils.register(new DateLocaleConverter(),  Date.class);

BeanUtils.setProperty(bean, "brithday", "2011-02-28");

System.out.println(bean.getBrithday());

}

@Test

public void test5() throws Exception{

Student bean=new Student();

ConvertUtils.register(new Converter() {

@Override

public Object convert(Class type, Object value) {

if(value==null){

return null;

}

SimpleDateFormat sdf=new  SimpleDateFormat("yyyy-MM-dd");

Date dt=null;

try {

dt=sdf.parse((String)value);

} catch (ParseException e) {

throw new ConversionException("日期格式转换有问 题....");

}

return dt;

}

}, Date.class);

BeanUtils.setProperty(bean, "brithday", "2011-02-28");

System.out.println(bean.getBrithday());

}

原创粉丝点击