Apache BeanUtils 1.9.2 官方入门文档
为什么需要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来命名
Employee employee = ...;
System.out.println("Hello " + employee.getFirstName() + "!");
- 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")); } }
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
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
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
// create the closure
BeanPropertyValueChangeClosure closure =
new BeanPropertyValueChangeClosure( "activeEmployee", Boolean.TRUE ); // update the Collection
CollectionUtils.forAllDo( peopleCollection, closure );
Querying Or Filtering Collections Of Beans
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate( "activeEmployee", Boolean.FALSE ); // filter the Collection
CollectionUtils.filter( peopleCollection, predicate );
Transforming Collections Of Beans
// create the transformer
BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer( "person.address.city" ); // transform the Collection
Collection peoplesCities = CollectionUtils.collect( peopleCollection, transformer );
Apache BeanUtils 1.9.2 官方入门文档的更多相关文章
- 【简明翻译】Hibernate 5.4 Getting Started Guide 官方入门文档
前言 最近的精力主要集中在Hibernate上,在意识到Hibernate 5 的中文资料并不多的时候,我不得不把目光转向Hibernate的官方doc,学习之余简要翻一下入门文档. 原文地址:htt ...
- vuex最简单、最直白、最全的入门文档
前言 我们经常用element-ui做后台管理系统,经常会遇到父组件给子组件传递数据,下面一个简单的例子,点击按钮,把弹框显示变量数据通过子组件的props属性传递,子组件通过$emit事件监听把数据 ...
- Ext js-02 -官方API文档使用
官方API文档地址: http://docs.sencha.com/extjs/6.5.3/classic/Ext.html 打开网页如下: 1.选择所使用的Ext js版本,后面offline do ...
- Umbraco官方技术文档 中文翻译
Umbraco 官方技术文档中文翻译 http://blog.csdn.net/u014183619/article/details/51919973 http://www.cnblogs.com/m ...
- Git教程(2)官方命令文档及常用命令表
http://www.cnblogs.com/angeldevil/archive/2013/11/26/3238470.html 1,官方命令文档 http://www.git-scm.com/do ...
- Azkaban2官方配置文档
最近工作实在是太忙了,我把之前翻译的官方的文档先放上来吧,希望对大家有所帮助~ 介绍 Azkaban2新功能: 1.Web UI 2.简单工作流上传 3.更容易设置job的依赖关系 4.调度工作流 5 ...
- FMDB官方使用文档-GCD的使用-提高性能(翻译)
FMDB官方使用文档-GCD的使用-提高性能(翻译) 发布于:2013-08-19 10:01阅读数:13395 由于FMDB是建立在SQLite的之上的,所以你至少也该把这篇文章从头到尾读一遍.与此 ...
- .Net 官方学习文档
.Net 官方学习文档:https://docs.microsoft.com/zh-cn/dotnet/articles/welcome
- Entity Framework Core 中文入门文档
点击链接查看文档: Entity Framework Core 中文入门文档
随机推荐
- SQL函数经常用到的mark一下
在项目开发过程中存储过程会用到很多SQL函数,经常用到的mark一下 1.经常用到的mark 一下 经常需要把id字符以','分隔传入存储过程然后SQL语句用in去搜索但是经常是这样的情况id 经常是 ...
- removeCss
(function ($) { //删除元素行内style中单个style和多个style //示例: //$(".b").removeCss("color") ...
- c语言(3)--运算符&表达式&语句
计算机的本职工作是进行一系列的运算,C语言为不同的运算提供了不同的运算符! 1.那些运算符们 .基本运算符 算术运算符:+ - * / % ++ -- 赋值运算符:= 逗号运算符:, 关系运算符:& ...
- Hash table in PowerShell
hashtable is easy to create, access and manipulate. we simply use $hashTable = @{} to create an empt ...
- (Problem 53)Combinatoric selections
There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...
- MinGW 编译zlib、libpng、libjpeg、libcurl等(全都是Qt项目)
MinGW 这里使用的是Qt5自带的MinGw版本,将路径D:\Qt\Qt5.1.0\Tools\mingw48_32\bin加到"环境变量"→"系统变量"→& ...
- CERC 2013 Magical GCD
题目大意如下:给定一个序列,每个序列有值xi,现给定t个数列,对于每个长n的数列,求一段[l,r]使 [r-l+1]*gcd(l,r)最大,gcd(l,r)指的是该连续区间的最大公约数. 不难想到n^ ...
- 代码收藏 JS实现页内查找定位功能
前部分为IE下搜索方法 用TextRange来实现 后部分为firefox.chrome下搜索方法 var nextIndex = 0; var searchValue = ''; var input ...
- PHP第一章学习——了解PHP(下)
继续昨天的部分! —————————————————————————————— 首先Ubuntu下安装Apache软件: ubuntu更新源有问题,又要解决半天! 我现在很冷静! 安装Apache教程 ...
- Why is celsius = 5 * (fahr - 32) / 9 ?
Go to my personal blog There is a program to print Fahrenheit-Celsius table as below. #include <s ...