`
muzitianqing
  • 浏览: 14274 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

commons-BeanUtils简介

阅读更多

Beanutils用了魔术般的反射技术,实现了很多夸张有用的功能,都是C/C++时代不敢想的。无论谁的项目,始终一天都会用得上它。我算是后知后觉了,第一回看到它的时候居然错过。

1.属性的动态getter,setter

在这框架满天飞的年代,不能事事都保证执行getter,setter函数了,有时候属性是要需要根据名字动态取得的,就像这样:  

BeanUtils.getProperty(myBean,"code");

BeanUtils更强的功能是直接访问内嵌对象的属性,只要使用点号分隔。

BeanUtils.getProperty(orderBean,"address.city");

相比之下其他类库的BeanUtils通常都很简单,不能访问内嵌的对象,所以经常要用CommonsBeanUtils替换它们。

BeanUtils还支持List和Map类型的属性。如下面的语法即可取得顾客列表中第一个顾客的名字

BeanUtils.getProperty(orderBean,"customers[1].name");

其中BeanUtils会使用ConvertUtils类把字符串转为Bean属性的真正类型,方便从HttpServletRequest等对象中提取bean,或者把bean输出到页面。

而PropertyUtils就会原色的保留Bean原来的类型。

2.beanCompartor动态排序

还是通过反射,动态设定Bean按照哪个属性来排序,而不再需要在bean的Compare接口进行复杂的条件判断。

Listpeoples=...;//Person对象的列表

Collections.sort(peoples,newBeanComparator("age"));

如果要支持多个属性的复合排序,如"OrderBylastName,firstName"

ArrayListsortFields=newArrayList();

sortFields.add(newBeanComparator("lastName"));

sortFields.add(newBeanComparator("firstName"));

ComparatorChainmultiSort=newComparatorChain(sortFields);

Collections.sort(rows,multiSort);

其中ComparatorChain属于jakatacommons-collections包。

如果age属性不是普通类型,构造函数需要再传入一个comparator对象为age变量排序。

另外,BeanCompartor本身的ComparebleComparator,遇到属性为null就会抛出异常,也不能设定升序还是降序。

这个时候又要借助commons-collections包的ComparatorUtils.

Comparatormycmp=ComparableComparator.getInstance();

mycmp=ComparatorUtils.nullLowComparator(mycmp);//允许null

mycmp=ComparatorUtils.reversedComparator(mycmp);//逆序

Comparatorcmp=newBeanComparator(sortColumn,mycmp);

3.Converter把Request或ResultSet中的字符串绑定到对象的属性

经常要从request,resultSet等对象取出值来赋入bean中,下面的代码谁都写腻了,如果不用MVC框架的绑定功能的话。

Stringa=request.getParameter("a");

bean.setA(a);

Stringb=....

不妨写一个Binder:

MyBeanbean=...;

HashMapmap=newHashMap();

Enumerationnames=request.getParameterNames();

while(names.hasMoreElements())

{

Stringname=(String)names.nextElement();

map.put(name,request.getParameterValues(name));

}

BeanUtils.populate(bean,map);

其中BeanUtils的populate方法或者getProperty,setProperty方法其实都会调用convert进行转换。

但Converter只支持一些基本的类型,甚至连java.util.Date类型也不支持。而且它比较笨的一个地方是当遇到不认识的类型时,居然会抛出异常来。

对于Date类型,我参考它的sqldate类型实现了一个Converter,而且添加了一个设置日期格式的函数。

要把这个Converter注册,需要如下语句:

ConvertUtilsBeanconvertUtils=newConvertUtilsBean();DateConverterdateConverter=newDateConverter();convertUtils.register(dateConverter,Date.class);//因为要注册converter,所以不能再使用BeanUtils的静态方法了,必须创建BeanUtilsBean实例BeanUtilsBeanbeanUtils=newBeanUtilsBean(convertUtils,newPropertyUtilsBean());beanUtils.setProperty(bean,name,value);

4其他功能

4.1PropertyUtils,当属性为Collection,Map时的动态读取:

Collection:提供index

BeanUtils.getIndexedProperty(orderBean,"items",1);

或者

BeanUtils.getIndexedProperty(orderBean,"items[1]");

Map:提供KeyValue

BeanUtils.getMappedProperty(orderBean,"items","111");//key-valuegoods_no=111

或者

BeanUtils.getMappedProperty(orderBean,"items(111)")

4.2PropertyUtils,获取属性的Class类型

publicstaticClassgetPropertyType(Objectbean,Stringname)

4.3ConstructorUtils,动态创建对象

publicstaticObjectinvokeConstructor(Classklass,Objectarg)

4.4MethodUtils,动态调用方法

MethodUtils.invokeMethod(bean,methodName,parameter);

4.5动态Bean见用DynaBean减除不必要的VO和FormBean

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics