Java高新技术之内省操作(BeanUtils、PropertyUtils和ConverUtils.register)

来源:互联网 发布:安卓自动接听软件 编辑:程序博客网 时间:2024/05/29 16:13

这篇博客是对JAVA内省JavaBean(Introspector、BeanInfo和PropertyDescriptor)的补充,里面提到了BeanUtils工具包中BeanUtilsPropertyUtilsConverUtils的使用。记住要导入两个包。

BeanUtils:以字符串的形式对javabean类的属性进行操作;

PropertyUtils:以属性本身的类型进行操作

ConverUtils:主要用到ConverUtils.regsiter,这个方法主要是转换非基本数据类型用的

三者的使用具体看主要看代码注释分析(主要结合我的那篇博客看)

先写个Person类:

public class Person {/* * 小知识点:请问Person类中有几个属性 * 有5个:分别是name、age、password、AA和class(Object中的getClass方法) * 一个自定义的类有几个属性不是由字段决定的,而是由getXXX()或setXXX()决定的 * */private String name;//字段private int age;//字段private int password;//字段private Date birthday;public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getAA(){return null;}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 int getPassword() {return password;}public void setPassword(int password) {this.password = password;}}

下面是对Person类的相关操作(用到了Junit测试)

public class TestPerson {//以前传统的做法:得到Person中bean的所有属性@Testpublic void test1()throws Exception{BeanInfo info=Introspector.getBeanInfo(Person.class);PropertyDescriptor[]  ps=info.getPropertyDescriptors();System.out.println(ps.length);for(PropertyDescriptor p:ps){//从打印的结果就知道,Person类中有5个属性System.out.println(p.getName());}}//操作bean特定属性:age@Testpublic void test2()throws Exception{PropertyDescriptor p=new PropertyDescriptor("age", Person.class);Method setMethod=p.getWriteMethod();Method getMethod=p.getReadMethod();Person pp=new Person();setMethod.invoke(pp, 23);System.out.println(getMethod.invoke(pp, null));}@Testpublic void test3()throws Exception{//BeanUtils:是以字符串形式对javabean进行操作,Person p=new Person();//它会自动的吧字符串形式的数组转换成int类型(只要是基本数据类型都可以)BeanUtils.setProperty(p, "age", "23");System.out.println("BeanUtils::"+p.getAge());//PropertyUtils:是以属性本身的类型进行操作PropertyUtils.setProperty(p, "age", 12);System.out.println("PropertyUtils::"+p.getAge());}@Testpublic void test4()throws Exception{String name="读卡机";int age=55;int password=1233121;Person p=new Person();BeanUtils.setProperty(p, "name", name);BeanUtils.setProperty(p, "age", age);BeanUtils.setProperty(p, "password", password);System.out.println(p.getName());System.out.println(p.getAge());System.out.println(p.getPassword());}@Testpublic void test5()throws Exception{String name="读卡机";int age=55;int password=1233121;String birthday="2012-3-12";//为了让Date类型的birthday能够赋值,我们需要给BeanUtils注册一个Date的转换器ConvertUtils.register(new Converter() {@Overridepublic Object convert(Class bean, Object value) {if(value==null){return null;}if(!(value instanceof String)){throw new ConversionException("不支持的格式");}String valueStr=(String) value;if(valueStr.trim().equals("")){return null;}SimpleDateFormat forma=new SimpleDateFormat("yyyy-MM-dd");//注意:这里只能try catch,因为接口Converter的convert方法没有抛异常,老子不抛,儿子怎么敢抛呢try {Date d=forma.parse(valueStr);return d;} catch (ParseException e) {throw new RuntimeException(e);}}}, Date.class);Person p=new Person();BeanUtils.setProperty(p, "name", name);BeanUtils.setProperty(p, "age", age);BeanUtils.setProperty(p, "password", password);//如果没有ConvertUtils.register会报错,因为birthday的属性是Date类型的,//不是基本数据类型,看test3怎么说的BeanUtils.setProperty(p, "birthday", birthday);System.out.println(p.getName());System.out.println(p.getAge());System.out.println(p.getPassword());System.out.println(p.getBirthday());}@Testpublic void test6()throws Exception{String name="读卡机";int age=55;int password=1233121;String birthday="2012-3-12";//优化test5/* 为了让Date类型的birthday能够赋值,我们需要给BeanUtils注册一个Date的转换器,下面这个转换器是beanutils工具包中写好的,我们可以直接使用,不过这个方法有bug举个例子:假如String birthday="";而没有实际的值,那么下面这个方法就会报错,而test5就不会因为我们在内部自己定义了方法,进行判断了。 * */ConvertUtils.register(new DateLocaleConverter(), Date.class);Person p=new Person();BeanUtils.setProperty(p, "name", name);BeanUtils.setProperty(p, "age", age);BeanUtils.setProperty(p, "password", password);//如果没有ConvertUtils.register会报错,因为birthday的属性是Date类型的,//不是基本数据类型,看test3怎么说的BeanUtils.setProperty(p, "birthday", birthday);System.out.println(p.getName());System.out.println(p.getAge());System.out.println(p.getPassword());System.out.println(p.getBirthday());}}



原创粉丝点击