为什么需要Apache BeanUtils?

Apache BeanUtils 是 Apache开源软件组织下面的一个项目,被广泛使用于Spring、Struts、Hibernate等框架,有数千个jar包依赖于它。它通过JDK中反射和自省的功能,提供了许多实用但JDK并未直接提供的功能。我找到了官方的入门文档,用自己的语言翻译出来,希望大家指正。

最早可能要从JavaBean说起,这个名称来源于一个针对组件架构的Java API,按照JavaBeans设计原则来编写Java类会让开发者更容易理解你的类所能提供的功能,就好像允许那些能够意识到JavaBeans的工具来使用Java的内省能力来知道你的类所提供的的属性和操作。并用一种具有视觉吸引力的方式展现在开发工具上(我的理解就是,在用Eclipse或IntelliJ idea时在对象名后面按小数点后会弹出方法列表 )。

JavaBeans规范定义了完整的特性集合来判断任意一个Java类是否是JavaBean,你应该考虑把阅读这个文档作为你Java编程技能的重要部分。部分重要特性如下:

  • 类的标志限定符必须是public,并且提供一个public的无参构造器。这将允许工具和应用来动态创建你的bean的新的实例,而不用提前知道哪一个Java类名将被使用。(关于这点,在StackOverFlow上有一个讨论,我也参与了回答)
  • 既然拥有一个无参构造器,那么配置bean的行为必须和初始化分离,这通常是通过定义一系列的属性。通过它们你可以修改bean的行为或数据。属性的命名通常是用驼峰命名法。
  • 通常,每个属性会分别有一个public的getter和setter方法来取得或是设置属性值。JavaBeans规范定义了命名惯例。
         public class Employee {
public Employee(); // Zero-arguments constructor
public String getFirstName();
public void setFirstName(String firstName);
public String getLastName();
public void setLastName(String lastName);
public Date getHireDate();
public void setHireDate(Date hireDate);
public boolean isManager();
public void setManager(boolean manager);
public String getFullName();
}
  • 对于boolean变量有一个例外,如果你觉得isManager比getManager更容易理解,你可以用isManager来命名
  标准的Java语法机制让你很容易地通过getFullName()取得Employee中的FullName。例如
 
Employee employee = ...;
System.out.println("Hello " + employee.getFirstName() + "!");
 
  但是当你在更复杂的环境中,你不一定能能提前知道哪个Bean将被使用,哪个属性要取得或被修改时,你怎么办?Java语言提供了像java.beans.Introspector的类,能够在运行时检查类,并确定属性的setter/getter方法名,再加上 Reflection 可以动态执行方法的能力 来做这样的事。但是,这些API很难用,还暴露了Java类底层很多不必要的细节。BeanUtils里的API就是设计用来在运行时简化它们,
而不是在编译的时候。
  所有JavaBean支持的属性类型可以被分为三类——一些是被JavaBean规范支持,一些是只被BeanUtils包接受。
  • Simple。  只有一个可以被取得或修改的值。int,java.lang.String,或是被Java语言、其他的引用或是类库所定义的更复杂的对象。
  • Indexed。 一个有下标的属性存储着一个有序的集合。
  • Mapped。 作为JavaBean APIs的扩展,BeanUtils认为任何拥有一个java.util.Map的值的属性都是"mapped"。你可以通过一个String的key来set/get单独的值。

  下面用一个自己写的例子来入门。粘贴进编辑器直接运行。

package beanUtils;

import java.util.HashMap;
import java.util.Map; /**
* Created by Andrew on 2015/12/4.
*/
public class Employee {
String firstName;
String lastName;
Employee[] subordinate;
Map<String, Address> address; public Employee(){
firstName = "Adnrew";
lastName = "Chen";
subordinate = new Employee[]{new Employee("Shirley","Liu"),new Employee("Alex","Wang")};
address = new HashMap<>();
address.put("home", new Address("Changsha YueLuShan"));
} private Employee(String firstName,String lastName){
this.firstName = firstName;
this.lastName = lastName;
} public Address getAddress(String type) {
return address.get(type);
} public void setAddress(String type, Address address) {
this.address.put(type, address);
} public Employee getSubordinate(int index) {
return subordinate[index];
} public void setSubordinate(int index, Employee subordinate) {
this.subordinate[index] = subordinate;
} public String getFirstName() {
return firstName;
} public void setFirstName(String firstName) {
this.firstName = firstName;
} public void setFirstName(Float fl){} public String getLastName() {
return this.lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
} @Override
public String toString() {
return firstName+" "+lastName;
}
}
package beanUtils;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils; import java.lang.reflect.InvocationTargetException; /**
* Created by Andrew on 2015/12/4.
*/
public class BeanUtilsTest { public static void main(String[] args) {
Employee employee = new Employee();
try { System.out.println((String) PropertyUtils.getSimpleProperty(employee, "firstName"));
System.out.println((String)PropertyUtils.getSimpleProperty(employee, "lastName"));
System.out.println(PropertyUtils.getIndexedProperty(employee, "subordinate[0]"));
System.out.println(PropertyUtils.getMappedProperty(employee, "address(home)"));
System.out.println(PropertyUtils.getNestedProperty(employee, "address(home).city")); } catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} }
}

对于Simple属性,直接调用PropertyUtils.getSimpleProperty(employee, "firstName")就可以获得。

对于Indexed属性,PropertyUtils.getIndexedProperty(employee, "subordinate[0]")或是PropertyUtils.getIndexedProperty(employee, "subordinate",0)来获得。

对于Mapped属性,PropertyUtils.getMappedProperty(employee, "address(home)")PropertyUtils.getMappedProperty(employee, "address","home")来获得。

对于更复杂的嵌套属性,假设获取employee的address中key="home"的对象的city属性。我们可以用标准的写法:

String city = employee.getAddress("home").getCity();

通过PropertyUtils,你可以像JavaScript那样通过分隔符 "." 来取得嵌套的属性

String city = (String) PropertyUtils.getNestedProperty(employee, "address(home).city");

更详细的可以查看文档API

动态Bean(DynaBeans)

  动态Bean的一个最常用法就是包裹SQL查询结果,而不用写一堆的JavaBean。

   Connection conn = ...;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery
("select account_id, name from customers");
Iterator rows = (new ResultSetDynaClass(rs)).iterator();
while (rows.hasNext()) {
DynaBean row = (DynaBean) rows.next();
System.out.println("Account number is " +
row.get("account_id") +
" and name is " + row.get("name"));
}
rs.close();
stmt.close();

这样我们就免去写customer.java这个类,还有数百个与此相似的类。

下面来逐个介绍包中的成员。

BasicDynaBean and BasicDynaClass

用一个例子简单介绍基本用法。

package beanUtils;

import org.apache.commons.beanutils.*;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; /**
* Created by Andrew on 2015/12/6.
*/
public class DynaBeanTest { public static void main(String[] args) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
DynaProperty[] properties = new DynaProperty[]{
new DynaProperty("address",java.util.Map.class),
new DynaProperty("subordinate",beanUtils.Employee[].class),
new DynaProperty("firstName",String.class),
new DynaProperty("lastName",String.class)
}; BasicDynaClass dynaBeanClass = new BasicDynaClass("employee",null,properties); DynaBean employee = dynaBeanClass.newInstance();
employee.set("address",new HashMap<>());
employee.set("subordinate",new Employee[0]);
employee.set("firstName",new String("Andrew"));
employee.set("lastName", new String("chen")); System.out.println(PropertyUtils.getProperty(employee, "firstName"));
System.out.println(PropertyUtils.getProperty(employee, "lastName")); } }
 
通过DynaProperty数组来定义动态Bean的属性,但如果企图get不存在的属性,会抛出NoSuchMethodException。如果企图set在DynaProperty中不存在的属性,会抛出IllegalArgumentException。
 

ResultSetDynaClass

这个类是用来包裹 java.sql.ResultSet 来简化代码,如前面实例所示。

RowSetDynaClass

这个类的存在是为了解决ResultSetDynaClass 的一个问题:处理结果集时需要保持ResultSet打开。这意味着数据库的连接也需要打开,我们没有简单的机制来确定连接最后会不会放回连接池,或是连接关闭了,因此在Strut这种提供MVC控制器的框架中就不适用(好吧,我也不懂为什么不适用)。使用RowSetDynaClass 可以将结果复制到内存中。

     Connection conn = ...;  // Acquire connection from pool
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT ...");
RowSetDynaClass rsdc = new RowSetDynaClass(rs);
rs.close();
stmt.close();
...; // Return connection to pool
List rows = rsdc.getRows();
...; // Process the rows as desired
 

WrapDynaBean and WrapDynaClass

当你觉得很方便地使用着动态获取属性方法的时候,忽然发现其实还有一堆的标准JavaBean存在于遗留代码中,而它们无法使用这种set/get的通用方法来操作。幸运的是,我们可以将它们包裹成动态Bean。

     MyBean bean = ...;
DynaBean wrapper = new WrapDynaBean(bean);
String firstName = wrapper.get("firstName");

Lazy DynaBeans

  懒人使用的DynaBean?不是啦,其实是一种更方便的DynaBean。当你set的属性不存在时,会自动地填上相关属性,因为LazyBean实现了 MutableDynaClass ,即可变的动态类。当Indexed属性容量不够时,会自动增长。用一个例子来说明。
package beanUtils;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.LazyDynaBean; /**
* Created by Andrew on 2015/12/6.
*/
public class LazyDynaBeanTest { public static void main(String[] args) {
DynaBean lazyBean = new LazyDynaBean();
lazyBean.set("foo","bar");
lazyBean.set("number",2);
lazyBean.set("truth",false);
lazyBean.set("object",new Object()); lazyBean.set("map","20112601604","Andrew");
lazyBean.set("map","20112601605","Frank"); lazyBean.set("index",0,3);
lazyBean.set("index", 1, "str"); System.out.println(lazyBean.get("index"));
lazyBean.set("mamama",true);
System.out.println(lazyBean.get("mamama")); }
}
 
 

Data Type Conversions

常见的一个应用场景是要把HttpRequest中的参数取出来组装成对象,这是Struts框架提供的功能之一,就是通过BeanUtils实现的。我们需要把String类型的属性转化成它内在的类型,如int或boolean。这可以通过BeanUtils的populate()方法来实现。
 
     HttpServletRequest request = ...;
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);
 

Collections

Operating On Collections Of Beans

  通过Closure 来对集合中所有元素进行同一个操作。
    // create the closure
BeanPropertyValueChangeClosure closure =
new BeanPropertyValueChangeClosure( "activeEmployee", Boolean.TRUE ); // update the Collection
CollectionUtils.forAllDo( peopleCollection, closure );
 

Querying Or Filtering Collections Of Beans

  通过Predicate 来对集合中所有元素进行过滤操作
     BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate( "activeEmployee", Boolean.FALSE ); // filter the Collection
CollectionUtils.filter( peopleCollection, predicate );

  

 

Transforming Collections Of Beans

   通过Transformer 来对集合中元素进行"变压"操作,从一个集合转换成另一个集合。
     // create the transformer
BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer( "person.address.city" ); // transform the Collection
Collection peoplesCities = CollectionUtils.collect( peopleCollection, transformer );
 
 

Apache BeanUtils 1.9.2 官方入门文档的更多相关文章

  1. 【简明翻译】Hibernate 5.4 Getting Started Guide 官方入门文档

    前言 最近的精力主要集中在Hibernate上,在意识到Hibernate 5 的中文资料并不多的时候,我不得不把目光转向Hibernate的官方doc,学习之余简要翻一下入门文档. 原文地址:htt ...

  2. vuex最简单、最直白、最全的入门文档

    前言 我们经常用element-ui做后台管理系统,经常会遇到父组件给子组件传递数据,下面一个简单的例子,点击按钮,把弹框显示变量数据通过子组件的props属性传递,子组件通过$emit事件监听把数据 ...

  3. Ext js-02 -官方API文档使用

    官方API文档地址: http://docs.sencha.com/extjs/6.5.3/classic/Ext.html 打开网页如下: 1.选择所使用的Ext js版本,后面offline do ...

  4. Umbraco官方技术文档 中文翻译

    Umbraco 官方技术文档中文翻译 http://blog.csdn.net/u014183619/article/details/51919973 http://www.cnblogs.com/m ...

  5. Git教程(2)官方命令文档及常用命令表

    http://www.cnblogs.com/angeldevil/archive/2013/11/26/3238470.html 1,官方命令文档 http://www.git-scm.com/do ...

  6. Azkaban2官方配置文档

    最近工作实在是太忙了,我把之前翻译的官方的文档先放上来吧,希望对大家有所帮助~ 介绍 Azkaban2新功能: 1.Web UI 2.简单工作流上传 3.更容易设置job的依赖关系 4.调度工作流 5 ...

  7. FMDB官方使用文档-GCD的使用-提高性能(翻译)

    FMDB官方使用文档-GCD的使用-提高性能(翻译) 发布于:2013-08-19 10:01阅读数:13395 由于FMDB是建立在SQLite的之上的,所以你至少也该把这篇文章从头到尾读一遍.与此 ...

  8. .Net 官方学习文档

    .Net 官方学习文档:https://docs.microsoft.com/zh-cn/dotnet/articles/welcome

  9. Entity Framework Core 中文入门文档

    点击链接查看文档: Entity Framework Core 中文入门文档

随机推荐

  1. android项目 之 记事本(12) ----- 图片的等比例缩放及给图片加入边框

    本文是自己学习所做笔记.欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 在Android的UI开发中常常会遇到图片的缩放,就比方记事本,如今的图片都比較 ...

  2. POJ 1850 Code(找规律)

    Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7913   Accepted: 3709 Description ...

  3. 下载文件 ,调用系统的方法(UIDocumentInteractionController) 查看

  4. Find the k-th Smallest Element in the Union of Two Sorted Arrays

    (http://leetcode.com/2011/01/find-k-th-smallest-element-in-union-of.html) Given two sorted arrays A, ...

  5. 图片剪切之Croppic插件

    前几天做图片上传时需要进行图片的剪切和缩放,上网查找时找到了这个插件.样式很好看,功能也很OK.但是网上都是php进行后台处理图片的例子,然后只好慢慢琢磨C#的处理.插件地址是:http://www. ...

  6. 初识python yield

    for sel in response.xpath('//ul/li'): item = DmozItem() item['title'] = sel.xpath('a/text()').extrac ...

  7. [LeetCode]题解(python):095-Unique Binary Search Trees II

    题目来源: https://leetcode.com/problems/unique-binary-search-trees-ii/ 题意分析: 给一个整数,返回所有中序遍历是1到n的树. 题目思路: ...

  8. HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误 #Reprinted#

    请求方法是请求一定的Web页面的程序或用于特定的URL. 可选用下列几种: GET: 请求指定的页面信息,并返回实体主体. HEAD: 只请求页面的首部. POST: 请求服务器接受所指定的文档作为对 ...

  9. w3c标准的selection对象介绍

    简介 术语 属性 方法 document.activeElement document.designMode = 'on'; 简介 selection是对当前激活选中区(即高亮文本)进行操作. 在非I ...

  10. Protel99se轻松入门:一些高级设置和常用技巧

    给PCB补泪滴的具体操作 这里我们可以知道给PCB做覆铜是多么的简单 在PCB中如何找到我们要找的封装 在PCB中如何打印出中空的焊盘(这个功能对于热转印制板比较有用) 如何在PCB文件中加上漂亮的汉 ...