1、 BeanUtils一共分4個包:
org.apache.commons.beanutils
org.apache.commons.beanutils.converters
org.apache.commons.beanutils.locale
org.apache.commons.beanutils.locale.converters
其中上面兩個是BeanUtils的預設實現,它沒有針對本地化的任何處理,這個可以提高執行效率。
但是若你的程式對於本地化有要求的話,那還是使用下面2個包比較安全。
org.apache.commons.beanutils
這個包主要提供用於操作JavaBean的工具類,Jakarta-Common-BeanUtils的主要功能都在這個包裡實現。
2、BeanUtils可以直接get和set一個屬性的值。它將property分成3種類型:
Simple——簡單類型,如Stirng、Int……
Indexed——索引類型,如數組、arrayList……
Maped——這個不用說也該知道,就是指Map啦,比如HashMap……
訪問不同類型的資料可以直接調用函數getProperty和setProperty。getProperty只有2個參數,第一個是JavaBean對象,第二個是要操作的屬性名稱;setProperty提供三個參數,第一是JavaBean對象,第二個是要操作的屬性名稱,第三個為要設定的具體的值
public class Company { private String name; private HashMap address = new HashMap(); private String[] otherInfo; private ArrayList product; private ArrayList employee; private HashMap telephone; public Company(){} public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress(String type) { return address.get(type).toString(); } public void setAddress(String type, String address) { this.address.put(type,address); } public String[] getOtherInfo() { return otherInfo; } public void setOtherInfo(String[] otherInfo) { this.otherInfo = otherInfo; } public ArrayList getProduct() { return product; } public void setProduct(ArrayList product) { this.product = product; } public ArrayList getEmployee() { return employee; } public void setEmployee(ArrayList employee) { this.employee = employee; } public HashMap getTelephone() { return telephone; } public void setTelephone(HashMap telephone) { this.telephone = telephone; } }
(1)Simple
//對於Simple類型,參數二直接是屬性名稱即可
System.out.println(BeanUtils.getProperty(c, "name"));
(2)Map
//對於Map類型,則需要以“屬性名稱(key值)”的形式
System.out.println(BeanUtils.getProperty(c, "address (A2)"));
HashMap am = new HashMap();
am.put("1","234-222-1222211");
am.put("2","021-086-1232323");
BeanUtils.setProperty(c,"telephone",am);
System.out.println(BeanUtils.getProperty(c, "telephone (2)"));
Map類型也可以採用BeanUtils.getMappedProperty(c, "address","A2");
(3)index
//對於Indexed,則為“屬性名稱[索引值]”,注意這裡對於ArrayList和數組都可以用一樣的方式進行操作。
System.out.println(BeanUtils.getProperty(c, "otherInfo[2]"));
BeanUtils.setProperty(c, "product[1]", "NOTES SERVER");
System.out.println(BeanUtils.getProperty(c, "product[1]"));
index類型還可以採用BeanUtils.getIndexedProperty(c,"product",1);
(4)nest 3種類也可以組合使用啦。
System.out.println(BeanUtils.getProperty(c, "employee[1].name"));
3、bean copy功能
Company c2 = new Company();
BeanUtils.copyProperties(c2, c);
但是這種copy都是淺拷貝,複製後的2個Bean的同一個屬性可能擁有同一個對象的ref,這個在使用時要小心,特別是對於屬性為自訂類的情況
4、 BeanUtils和PropertyUtils
這兩個類幾乎有一摸一樣的功能,唯一的區別是:BeanUtils在對Bean賦值是會進行類型轉化。舉例來說也就是在copyProperty時只要屬性名稱相同,就算類型不同,BeanUtils也可以進行copy;而PropertyBean則可能會報錯。。
針對上面的例子,建立一個Company2的類,其中代碼與Company一樣,只是將otherinfo從String[]改為String。
Company c = init();
Company2 c2 = new Company2();
BeanUtils.copyProperties(c2,c);
// PropertyUtils.copyProperties(c2,c); 這句會報錯。。
System.out.println(c2.getOtherInfo());
當然2個Bean之間的同名屬性的類型必須是可以轉化的,否則用BeanUtils一樣會報錯。
若實現了org.apache.commons.beanutils.Converter介面則可以自訂類型之間的轉化。
由於不做類型轉化,用PropertyUtils在速度上會有很大提高。
此外,不作類型轉化還有一個好處,如下面的代碼:
//test data type convert
// ArrayList a1 = BeanUtils.getProperty(c,"product"); //BeanUtils返回的是String
System.out.println("--" + BeanUtils.getProperty(c,"product")); //取出後直接被轉為String
ArrayList a = (ArrayList)PropertyUtils.getProperty(c,"product");//PropertyUtils返回的是Object
System.out.println("--" + a.get(1));
用BeanUtils無法返回一個對象(除非自己寫一個Converter),它會自動進行類型轉化,然後返回String。對於想返回java類或自訂類的話,還是請使用PropertyUtils
5、Utils類
所有的XXXUtils類都提供的是靜態方法,可以直接調用,其主要實現都在相應的XXXUtilsBean中:
BeanUtils ——> BeanUtilsBean
ConvertUtils ——> ConvertUtilsBean
PropertyUtils ——> PropertyUtilsBean
(1)PropertyUtils,擷取屬性的Class類型
public static Class getPropertyType(Object bean, String name)
(2)ConstructorUtils,動態建立對象
public static Object invokeConstructor(Class klass, Object arg)
(3)MethodUtils,動態調用方法
MethodUtils.invokeMethod(bean, methodName, parameter);