Beanutils用了魔术般的反射技术,实现了很多夸张有用的功能,都是C/C++时代不敢想的。无论谁的项目,始终一天都会用得上它。我算是后知后觉了,第一回看到它的时候居然错过。
1.属性的动态getter、setter
在这框架满天飞的年代,不能事事都保证执行getter,setter函数了,有时候属性是要根据名字动态取得的,就像这样:
BeanUtils.getProperty(myBean,"code");
而Common BeanUtils的更强功能在于可以直接访问内嵌对象的属性,只要使用点号分隔。
BeanUtils.getProperty(orderBean, "address.city");
相比之下其他类库的BeanUtils通常都很简单,不能访问内嵌的对象,所以有时要用Commons BeanUtils来替换它们。
BeanUtils还支持List和Map类型的属性,如下面的语法即可取得Order的顾客列表中第一个顾客的名字
BeanUtils.getProperty(orderBean, "customers[1].name");
其中BeanUtils会使用ConvertUtils类把字符串转为Bean属性的真正类型,方便从HttpServletRequest等对象中提取bean,或者把bean输出到页面。
而PropertyUtils就会原色的保留Bean原来的类型。
2.BeanCompartor 动态排序
还是通过反射,动态设定Bean按照哪个属性来排序,而不再需要在实现bean的Compare接口进行复杂的条件判断。
List peoples = ...; // Person对象的列表
Collections.sort(peoples, new BeanComparator("age"));
如果要支持多个属性的复合排序,如"Order By lastName,firstName"
ArrayList sortFields = new ArrayList();
sortFields.add(new BeanComparator("lastName"));
sortFields.add(new BeanComparator("firstName"));
ComparatorChain multiSort = new ComparatorChain(sortFields);
Collections.sort(rows,multiSort);
其中ComparatorChain属于jakata commons-collections包。
如果age属性不是普通类型,构造函数需要再传入一个comparator对象为age变量排序。
另外, BeanCompartor本身的ComparebleComparator, 遇到属性为null就会抛出异常, 也不能设定升序还是降序。这个时候又要借助commons-collections包的ComparatorUtils.
Comparator mycmp = ComparableComparator.getInstance();
mycmp = ComparatorUtils.nullLowComparator(mycmp); //允许null
mycmp = ComparatorUtils.reversedComparator(mycmp); //逆序
Comparator cmp = new BeanComparator(sortColumn, mycmp);
3.Converter 把Request或ResultSet中的字符串绑定到对象的属性
经常要从request,resultSet等对象取出值来赋入bean中,如果不用MVC框架的绑定功能的话,下面的代码谁都写腻了。
String a = request.getParameter("a");
bean.setA(a);
String b = ....
bean.setB(b);
......
不妨写一个Binder自动绑定所有属性:
MyBean bean = ...;
HashMap map = new HashMap();
Enumeration names = request.getParameterNames();
while (names.hasMoreElements())
{
String name = (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注册,需要如下语句:
ConvertUtilsBean convertUtils = new ConvertUtilsBean();
DateConverter dateConverter = new DateConverter();
convertUtils.register(dateConverter,Date.class);
//因为要注册converter,所以不能再使用BeanUtils的静态方法了,必须创建BeanUtilsBean实例
BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils,new PropertyUtilsBean());
beanUtils.setProperty(bean, name, value);
4 其他功能
4.1 ConstructorUtils,动态创建对象
public static Object invokeConstructor(Class klass, Object arg)
4.2 MethodUtils,动态调用方法
MethodUtils.invokeMethod(bean, methodName, parameter);
4.3 PropertyUtils,当属性为Collection,Map时的动态读取:
Collection: 提供index
BeanUtils.getIndexedProperty(orderBean,"items",1);
或者
BeanUtils.getIndexedProperty(orderBean,"items[1]");
Map: 提供Key Value
BeanUtils.getMappedProperty(orderBean, "items","111");//key-value goods_no=111
或者
BeanUtils.getMappedProperty(orderBean, "items(111)")
4.4 PropertyUtils,直接获取属性的Class类型
public static Class getPropertyType(Object bean, String name)
4.5 动态Bean 见
用DynaBean减除不必要的VO和FormBean
分享到:
相关推荐
使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。
common-beanutils下载地址:http://commons.apache.org/proper/commons-beanutils/ common-logging现在地址:http://commons.apache.org/proper/commons-logging/
Commons-beanutils jar包,比较新希望对大家能有帮助
commons-beanutils (1.7.0 -1.9.2) 6个jar包. 资源共享,有需要其他jar包的可以在评论留言,看到后我会陆续上传。
commons-beanutils-1.8.0 jar包: 1.commons-beanutils-1.8.0.jar 2.commons-beanutils-1.8.0-javadoc.jar 3.commons-beanutils-1.8.0-sources.jar 4.commons-beanutils-bean-collections-1.8.0.jar 5.commons-...
BeanUtils是由Apache公司开发的针对操作JavaBean的工具包。...但是由Apache公司开发的BeanUtils会更常用,同时,BeanUtils还需要配合第三方日志工具来使用,这里我们同样使用Apache公司的common logging
commons-beanutils.jar包及源码文件,方便使用
commons-beanutils-1.8.3.rar官方正版免费版,BeanUtils主要提供了对于JavaBean进行各种操作。 个包主要提供用于操作JavaBean的工具类,Jakarta-Common-BeanUtils的主要功能都在这个包里实现。
解决办法是把commons-beanutils中的org/apache/commons/collections删除,这里上传一个改好可以直接使用的。
这个包主要提供用于操作JavaBean的工具类,Jakarta-Common-BeanUtils的主要功能都在这个包里实现。
BeanUtils.copyProperteis(dest,orgi); 属性拷贝
Commons是Apache开放源代码组织中的一个Java子... 由于Fileupload子项目同时要用到另外一个项目commons-Beanutils,所以必须下载Beanutils,并将解压后的文件commons-beanutils.jar拷贝到{$TOMCAT}/common/lib目录下。
以下是压缩文件的jar包名称: commons-validator-1.3.0.jar commons-pool-1.3.jar ...commons-beanutils-1.6.jar 包含两个最常用的源码: commons-beanutils-1.6-src.zip commons-collections-3.2.1-src.zip
commons-beanutils-1.7.0
基本上涵盖了所有common包):commons-validator.jar commons-validator-1.3.1.jar commons-pool.jar commons-...
beanUtils配套的commons-logging
commons BeanUtils 这些包,全部通用于公司的大型商业项目,压缩包中包含了 1.4/1.5/1.6/1.7/1.8/1.9各个版本,下载资源超值
内含以下文件: c3p0-0.9.1.2.jar commons-dbutils-1.3.jar jstl.jar mysql-connector-java-5.1.7-bin.jar standard.jar
apache commons api beanutils collections configuration lang logging