`
yalong9527
  • 浏览: 76882 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Beanutils基本用法[转载]

    博客分类:
  • JAVA
阅读更多
Beanutils基本用法

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

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

BeanUtils.getProperty(myBean,"code");

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

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

相比之下其他类库的BeanUtils通常都很简单,不能访问内嵌的对象,所以经常要用Commons BeanUtils替换它们。
BeanUtils还支持List和Map类型的属性。如下面的语法即可取得顾客列表中第一个顾客的名字

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 = ....

不妨写一个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 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.2 PropertyUtils,获取属性的Class类型

     public static Class getPropertyType(Object bean, String name)


4.3 ConstructorUtils,动态创建对象

      public static Object invokeConstructor(Class klass, Object arg)

4.4 MethodUtils,动态调用方法

    MethodUtils.invokeMethod(bean, methodName, parameter);

4.5 动态Bean 见用DynaBean减除不必要的VO和FormBean
分享到:
评论

相关推荐

    Beanutils基本用法.doc

    Beanutils基本用法.doc 。一些常用方法的介绍。

    BeanUtils用法

    BeanUtils用法 BeanUtils是Apache Commons项目的一个子项目,提供了许多实用的方法来操作Java Bean。下面将详细讲解BeanUtils的用法和相关知识点。 概述 BeanUtils包是Apache Commons项目的一个子项目,最初是作为...

    BeanUtils_beanutils_

    BeanUtils 是 Apache commons组件的成员之一,主要用于简化JavaBean封装数据的操作。它可以给JavaBean封装一个字符串数据,也可以将一个表单提交的所有数据封装到JavaBean中。使用第三方工具,需要导入jar包:

    commons-beanutils-1.9.4-API文档-中文版.zip

    赠送jar包:commons-beanutils-1.9.4.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    commons-beanutils-1.9.3

    BeanUtils工具由Apache软件基金组织编写,提供给我们使用,主要解决的问题是:把对象的属性数据封装到对象中。在整个J2EE的编程过程中,我们经常会从各种配置文件中读取相应的数据,需要明白的一点是从配置文件中...

    BeanUtils工具

    BeanUtils工具是一种方便我们对JavaBean进行操作的工具,是Apache组织下的产品。beanUtils 可以便于对javaBean的属性、对象进行赋值,beanUtils可以将一个MAP集合的数据拷贝到一个javabean对象中。

    BeanUtils教程

    BeanUtils教程BeanUtils教程BeanUtils教程BeanUtils教程BeanUtils教程BeanUtils教程

    BeanUtils_1.8.3帮助文档

    BeanUtils_1.8.3最新帮助文档,希望对给位有用。。。

    commons-beanutils-1.9.4-API文档-中英对照版.zip

    使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    beanutils-1.9.3-bin

    BeanUtils是由Apache公司开发的针对操作JavaBean的工具包。...但是由Apache公司开发的BeanUtils会更常用,同时,BeanUtils还需要配合第三方日志工具来使用,这里我们同样使用Apache公司的common logging

    commons-beanutils-1.8.0.jar beanutils.jar beanutils.jar工具包

    commons-beanutils-1.8.0.jar beanutils.jar beanutils.jar工具包

    BeanUtils所需的包

    BeanUtils的包BeanUtils的包BeanUtils的包BeanUtils的包BeanUtils的包BeanUtils的包BeanUtils的包BeanUtils的包BeanUtils的包

    commons-beanutils-1.9.3-API文档-中文版.zip

    赠送jar包:commons-beanutils-1.9.3.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    Commons-beanutils jar包

    Commons-beanutils jar包,比较新希望对大家能有帮助

    commons-beanutils-1.8.0 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中文API.doc

    Commons-beanutil中包含大量和JavaBean操作有关的工具方法,使用beanutils可轻松利用Java反射机制来完成所需功能,而不需要详细研究反射的原理和使用,同时beanutils类库中提出了动态Bean的概念,可在运行时动态对...

    beanutils 1.8 jar包+源码_阿锦—小白

    完美beanUtils 1.8 jar包与源码

    BeanUtils的jar包

    BeanUtils的jar包

    commons-beanutils-1.9.1-API文档-中文版.zip

    赠送jar包:commons-beanutils-1.9.1.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    commons-beanutils-1.9.2 最新版本

    commons-beanutils-1.9.2

Global site tag (gtag.js) - Google Analytics