BeanUtils簡要描述
beanutils,顧名思義,是java bean的一個工具類,可以幫助我們方便的讀取(get)和設置(set)bean屬性值、動態定義和訪問bean屬性;
細心的話,會發現其實JDK已經提供了一個java.beans包,同樣可以實現以上功能,只不過使用起來比較麻煩,所以誕生了apache commons beanutils;
看源碼就知道,其實apache commons beanutils就是基於jdk的java.beans包實現的。
Java Bean
在介紹apache commons beanutils之前,很有必要先了解下javabean。
apache commons beanutils就是基於JavaBeans的設計命名規范來實現的,如下是一個簡單的javabean示例:
/* * File Name: Employee.java * Description: * Author: PiChen * Create Date: 2017年5月29日 */ package apache.commons.beanutils.example.pojo; import java.util.Date; /** * * @author PiChen * @version 2017年5月29日 */ public class Employee { private String firstName; private String lastName; private Date hireDate; private boolean isManager;/** * @return the firstName */ public String getFirstName() { return firstName; } /** * @param firstName the firstName to set */ public void setFirstName(String firstName) { this.firstName = firstName; } /** * @return the lastName */ public String getLastName() { return lastName; } /** * @param lastName the lastName to set */ public void setLastName(String lastName) { this.lastName = lastName; } /** * @return the hireDate */ public Date getHireDate() { return hireDate; } /** * @param hireDate the hireDate to set */ public void setHireDate(Date hireDate) { this.hireDate = hireDate; } /** * @return the isManager */ public boolean isManager() { return isManager; } /** * @param isManager the isManager to set */ public void setManager(boolean isManager) { this.isManager = isManager; } /** * @return the fullName */ public String getFullName() { return firstName + " " + lastName; } }
javabean一般有以下幾個特性:
1、類必須是public訪問權限,且需要有一個public的無參構造方法,之所以這樣主要是方便利用Java的反射動態創建對象實例:
Class beanClass = Class.forName(className);
Object beanInstance = beanClass.newInstance();
2、由於javabean的構造方法是無參的,所以我們的bean的行為配置(即設置bean的屬性值,方法對應行為,屬性對應數據)不能在構造方法完成,取而代之的是通過一系列的set方法來設置屬性值,通過setter方法,我們可以改變javabean呈現出來的行為和內部數據,這里的setter方法會按一定的約定來命名,如setHireDate、setName。。。
3、讀取和設置bean屬性值的命名約定,即getter方法和setter方法,不過這里需要特別注意boolean類型的約定,如下示例:
private String firstName; private String lastName; private Date hireDate; private boolean isManager; 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);
4、並不是必須為每個屬性提供setter和getter方法,我們可以只定義一個屬性的getter方法而不定義setter方法,這樣的屬性一般是只讀屬性;
訪問基本數據類型的Bean屬性
簡述:
這類屬性指的是Integer, Double, Float, boolean等,,,, 注意這里還包括String,其實像HashMap,ArrayList, 等屬性都可以設置,只不過Map里面的鍵值對、List索引處的值無法通過這兩個API訪問,需要使用專門的API來處理,接下來將會介紹;
訪問API:
PropertyUtils.getSimpleProperty(Object, String)
PropertyUtils.setSimpleProperty(Object, String, Object)
調用示例:
/* * File Name: Main.java * Description: * Author: PiChen * Create Date: 2017年5月29日 */ package apache.commons.beanutils.example.propertyaccess; import java.lang.reflect.InvocationTargetException; import org.apache.commons.beanutils.PropertyUtils; import apache.commons.beanutils.example.pojo.Employee; /** * * @author PiChen * @version 2017年5月29日 */ public class BasicPropertyAccess { /** * * * @param args * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalAccessException */ public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Employee employee = new Employee(); String firstName = (String) PropertyUtils.getSimpleProperty(employee, "firstName"); String lastName = (String) PropertyUtils.getSimpleProperty(employee, "lastName"); firstName = firstName == null ? "Pi" : ""; lastName = lastName == null ? "Chen" : ""; PropertyUtils.setSimpleProperty(employee, "firstName", firstName); PropertyUtils.setSimpleProperty(employee, "lastName", lastName); System.out.println(employee.getFullName()); } }
訪問索引類型的Bean屬性
簡述:
可索引的屬性,如ArrayList, 數組等,可以通過下標索引來訪問Bean屬性的值, 同理可設置value;
訪問API
PropertyUtils.getIndexedProperty(Object, String)
PropertyUtils.getIndexedProperty(Object, String, int)
PropertyUtils.setIndexedProperty(Object, String, Object)
PropertyUtils.setIndexedProperty(Object, String, int, Object)
調用示例
Bean:

package apache.commons.beanutils.example.pojo; import java.util.List; public class IndexedBean { private List<Employee> employeeList; private Integer[] intArr; /** * @return the employeeList */ public List<Employee> getEmployeeList() { return employeeList; } /** * @param employeeList the employeeList to set */ public void setEmployeeList(List<Employee> employeeList) { this.employeeList = employeeList; } /** * @return the intArr */ public Integer[] getIntArr() { return intArr; } /** * @param intArr the intArr to set */ public void setIntArr(Integer[] intArr) { this.intArr = intArr; } }
調用example:
/* * File Name: Main.java * Description: * Author: PiChen * Create Date: 2017年5月29日 */ package apache.commons.beanutils.example.propertyaccess; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; import org.apache.commons.beanutils.PropertyUtils; import apache.commons.beanutils.example.pojo.Employee; import apache.commons.beanutils.example.pojo.IndexedBean; /** * * @author PiChen * @version 2017年5月29日 */ public class IndexedPropertiesAccess { /** * * * @param args * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalAccessException */ public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { // 初始工作 IndexedBean indexedBean = new IndexedBean(); List<Employee> employeeList = new ArrayList<Employee>(); Employee e1 = new Employee(); e1.setLastName("Chen"); Employee e2 = new Employee(); e2.setLastName("Wang"); employeeList.add(e1); employeeList.add(e2); indexedBean.setEmployeeList(employeeList); indexedBean.setIntArr(new Integer[]{ 0, 1, 2 }); // API測試 int index0 = 0; String name0 = "employeeList[" + index0 + "]"; Employee employee0 = (Employee) PropertyUtils.getIndexedProperty(indexedBean, name0); System.out.println(employee0.getLastName()); int index1 = 1; String name1 = "employeeList[" + index1 + "]"; Employee employee1 = (Employee) PropertyUtils.getIndexedProperty(indexedBean, name1); System.out.println(employee1.getLastName()); Employee employee00 = (Employee) PropertyUtils.getIndexedProperty(indexedBean,"employeeList", 0); Employee employee11 = (Employee) PropertyUtils.getIndexedProperty(indexedBean,"employeeList", 1); System.out.println(employee00.getLastName()); System.out.println(employee11.getLastName()); Integer i = (Integer) PropertyUtils.getIndexedProperty(indexedBean,"intArr", 1); System.out.println(i); } }
訪問Map映射類型的Bean屬性
簡述:
常見的HashMap,TreeMap等,可以通過key來訪問Bean屬性值,同理可設置value;
訪問API:
PropertyUtils.getMappedProperty(Object, String)
PropertyUtils.getMappedProperty(Object, String, String)
PropertyUtils.setMappedProperty(Object, String, Object)
PropertyUtils.setMappedProperty(Object, String, String, Object)
調用示例:
map bean:

/* * File Name: MappedBean.java * Description: * Author: PiChen * Create Date: 2017年5月29日 */ package apache.commons.beanutils.example.pojo; import java.util.Map; /** * * @author PiChen * @version 2017年5月29日 */ public class MappedBean { private Map<String, Object> mapProperty; /** * @return the mapProperty */ public Map<String, Object> getMapProperty() { return mapProperty; } /** * @param mapProperty the mapProperty to set */ public void setMapProperty(Map<String, Object> mapProperty) { this.mapProperty = mapProperty; } }
使用example:
/* * File Name: MapPropertyAccess.java * Description: * Author: PiChen * Create Date: 2017年5月29日 */ package apache.commons.beanutils.example.propertyaccess; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.PropertyUtils; import apache.commons.beanutils.example.pojo.MappedBean; /** * * @author PiChen * @version 2017年5月29日 */ public class MapPropertyAccess { public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { MappedBean employee = new MappedBean(); Map<String, Object> map = new HashMap<String, Object>(); //employee.setMapProperty(map); PropertyUtils.setSimpleProperty(employee, "mapProperty", map); PropertyUtils.setMappedProperty(employee, "mapProperty", "testKey1", "testValue1"); PropertyUtils.setMappedProperty(employee, "mapProperty(testKey2)", "testValue2"); System.out.println(employee.getMapProperty().get("testKey1")); System.out.println(employee.getMapProperty().get("testKey2")); } }
訪問嵌套類型的Bean屬性
簡述:
指的是對象內嵌套對象
訪問API:
PropertyUtils.getNestedProperty(Object, String)
PropertyUtils.setNestedProperty(Object, String, Object)
調用示例:
嵌套bean:

/* * File Name: NestedBean.java * Description: * Author: PiChen * Create Date: 2017年5月29日 */ package apache.commons.beanutils.example.pojo; import java.util.List; import java.util.Map; /** * * @author PiChen * @version 2017年5月29日 */ public class NestedBean { private List<Employee> listProperty; private Map<String, Employee> mapProperty; /** * @return the listProperty */ public List<Employee> getListProperty() { return listProperty; } /** * @param listProperty the listProperty to set */ public void setListProperty(List<Employee> listProperty) { this.listProperty = listProperty; } /** * @return the mapProperty */ public Map<String, Employee> getMapProperty() { return mapProperty; } /** * @param mapProperty the mapProperty to set */ public void setMapProperty(Map<String, Employee> mapProperty) { this.mapProperty = mapProperty; } }
使用example:
/* * File Name: NestedPropertyAccess.java * Description: * Author: PiChen * Create Date: 2017年5月29日 */ package apache.commons.beanutils.example.propertyaccess; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.beanutils.PropertyUtils; import apache.commons.beanutils.example.pojo.Employee; import apache.commons.beanutils.example.pojo.NestedBean; /** * * @author PiChen * @version 2017年5月29日 */ public class NestedPropertyAccess { public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Employee e = new Employee(); e.setLastName("Chen"); NestedBean nestedBean = new NestedBean(); List<Employee> list = new ArrayList<Employee>(); list.add(e); Map<String, Employee> map = new HashMap<String, Employee>(); map.put("testKey", e); nestedBean.setListProperty(list); nestedBean.setMapProperty(map); String lastName = (String) PropertyUtils.getNestedProperty(nestedBean, "mapProperty(testKey).lastName"); System.out.println(lastName); String lastName2 = (String) PropertyUtils.getNestedProperty(nestedBean, "listProperty[0].lastName"); System.out.println(lastName2); } }
訪問所有類型的Bean屬性
簡述
通過以上API的使用,我們了解了各類bean屬性的訪問方法,其實還有一種通用的方法,適用於各類bean屬性類型;
訪問API
使用示例,這里直接以嵌套類型屬性為例
/* * File Name: NestedPropertyAccess.java * Description: * Author: PiChen * Create Date: 2017年5月29日 */ package apache.commons.beanutils.example.propertyaccess; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.beanutils.PropertyUtils; import apache.commons.beanutils.example.pojo.Employee; import apache.commons.beanutils.example.pojo.NestedBean; /** * * @author PiChen * @version 2017年5月29日 */ public class NestedPropertyAccess { public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Employee e = new Employee(); e.setLastName("Chen"); NestedBean nestedBean = new NestedBean(); List<Employee> list = new ArrayList<Employee>(); list.add(e); Map<String, Employee> map = new HashMap<String, Employee>(); map.put("testKey", e); nestedBean.setListProperty(list); nestedBean.setMapProperty(map); String lastName2 = (String) PropertyUtils.getProperty(nestedBean, "listProperty[0].lastName"); System.out.println(lastName2); PropertyUtils.setProperty(nestedBean, "listProperty[0].lastName", "Hello World"); System.out.println(nestedBean.getListProperty().get(0).getLastName()); } }
參考資料
源碼鏈接:
https://github.com/peterchenhdu/apache-commons-beanutils-example