为什么需要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 );