1. 概述
第一次接觸BeanUtils是在學(xué)習(xí)Struts的過程中,在Struts中它被大量用于處理FormBean。
BeanUtils主要提供了對(duì)于JavaBean進(jìn)行各種操作,
BeanUtils一共分4個(gè)包:
? org.apache.commons.beanutils
? org.apache.commons.beanutils.converters
? org.apache.commons.beanutils.locale
? org.apache.commons.beanutils.locale.converters
其中上面兩個(gè)是BeanUtils的默認(rèn)實(shí)現(xiàn),它沒有針對(duì)本地化的任何處理,這個(gè)可以提高執(zhí)行效率。但是若你的程序?qū)τ诒镜鼗幸蟮脑挘沁€是使用下面2個(gè)包比較安全。
2. org.apache.commons.beanutils
這個(gè)包主要提供用于操作JavaBean的工具類,Jakarta-Common-BeanUtils的主要功能都在這個(gè)包里實(shí)現(xiàn)。
下面分別介紹幾個(gè)主要的工具類:
1、首先,我先定義一個(gè)JavaBean作為之后例子的操作對(duì)象。
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;
}
}
2、BeanUtils可以直接get和set一個(gè)屬性的值。它將property分成3種類型:
Simple——簡單類型,如Stirng、Int……
Indexed——索引類型,如 數(shù)組、arrayList……
Maped——這個(gè)不用說也該知道,就是指Map啦,比如HashMap……
訪問不同類型的數(shù)據(jù)可以直接調(diào)用函數(shù)getProperty和setProperty。它們都只有2個(gè)參數(shù),第一個(gè)是JavaBean對(duì)象,第二個(gè)是要操作的屬性名。
Company c = new Company();
c.setName("Simple");
對(duì)于Simple類型,參數(shù)二直接是屬性名即可
//Simple
System.out.println(BeanUtils.getProperty(c, "name"));
對(duì)于Map類型,則需要以“屬性名(key值)”的形式
//Map
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)"));
對(duì)于Indexed,則為“屬性名[索引值]”,注意這里對(duì)于ArrayList和數(shù)組都可以用一樣的方式進(jìn)行操作。
//index
System.out.println(BeanUtils.getProperty(c, "otherInfo[2]"));
BeanUtils.setProperty(c, "product[1]", "NOTES SERVER");
System.out.println(BeanUtils.getProperty(c, "product[1]"));
當(dāng)然這3種類也可以組合使用啦!
//nest
System.out.println(BeanUtils.getProperty(c, "employee[1].name"));
3、此外,還有一個(gè)很重要的方法copyProperty,可以直接進(jìn)行Bean之間的clone。
Company c2 = new Company();
BeanUtils.copyProperties(c2, c);
但是這種copy都是淺拷貝,復(fù)制后的2個(gè)Bean的同一個(gè)屬性可能擁有同一個(gè)對(duì)象的ref,這個(gè)在使用時(shí)要小心,特別是對(duì)于屬性為類的情況。
4、最后還有populate,它用于將一個(gè)map的值填充到一個(gè)bean中,其函數(shù)原型如下:
public void populate(java.lang.Object bean,
java.util.Map properties)
throws java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException
在struts中這個(gè)函數(shù)被用于從http request中取得參數(shù)添加到FormBean,目前好像我也沒有看到這個(gè)函數(shù)還有什么其他的用途?!以后想到再說吧:P
它實(shí)現(xiàn)一個(gè)動(dòng)態(tài)的Bean,可以直接往里面加入屬性,作為一個(gè)JavaBean一樣使用,也可以用上面的BeanUtils或get/set方法進(jìn)行操作,而不用事先定義一個(gè)標(biāo)準(zhǔn)的JavaBean類啦:)
記得在《J2ee設(shè)計(jì)模式》中有一種Value Object的模式,用于在MVC各層之間傳遞數(shù)據(jù),避免直接傳遞大業(yè)務(wù)對(duì)象引起的性能問題,為了避免在項(xiàng)目中出現(xiàn)很多Bean類,在書中提供了一個(gè)動(dòng)態(tài)Value Object的實(shí)現(xiàn)(通過擴(kuò)展Map)。這里LazyDynaBean則可以作為一種更加成熟、穩(wěn)定的實(shí)現(xiàn)來使用:P
言歸正傳,LazyBean的確提供了一個(gè)很不錯(cuò)的DynaBean的實(shí)現(xiàn),用起來幾乎不需要寫什么多余的代碼^_^,下面就看看使用的例子吧!
//這里使用LazyDynaMap,它是LazyBean的一個(gè)輕量級(jí)實(shí)現(xiàn)
LazyDynaMap dynaBean1 = new LazyDynaMap();
dynaBean1.set("foo", "bar"); // simple
dynaBean1.set("customer", "title", "Mr"); // mapped
dynaBean1.set("address", 0, "address1"); // indexed
System.out.println(dynaBean1.get("address",0));
Map myMap = dynaBean1.getMap(); // retrieve the Map
System.out.println(myMap.toString());
上面的例子可以看到,它可以在set時(shí)自動(dòng)增加bean的property(既賦值的同時(shí)增加Bean中的property),同時(shí)也支持3中類型的property,并且LazyDynaMap還可以導(dǎo)出為map。
對(duì)于這個(gè)類還有兩個(gè)重要的Field要注意:
returnnull——指定在get方法使用了一個(gè)沒有定義過的property時(shí),DynaBean的行為。
//取的字段的信息
dynaBean1.setReturnNull(true);//設(shè)為ture。若Bean中沒有此字段,返回null
//默認(rèn)為false。若Bean中沒有此字段,自動(dòng)增加一個(gè):)
System.out.println(dynaBean1.get("aaa"));//此時(shí)返回null
Restricted——指定是否允許改變這個(gè)bean的property。
//MutableDynaClass.setRestricted設(shè)為true后,字段不可再增刪和修改.
//默認(rèn)為false,允許增刪和修改
dynaBean1.setRestricted(true);
dynaBean1.set("test","error");//這里會(huì)出錯(cuò)!
通過設(shè)置這兩個(gè)屬性,可以防止意外修改DynaBean的property。在設(shè)計(jì)架構(gòu)時(shí),你可以在后臺(tái)從數(shù)據(jù)表或xml文件自動(dòng)產(chǎn)生DynaBean,在傳到控制層和表示層之前設(shè)置上述屬性使其Bean結(jié)構(gòu)不允許修改,如此就不可能無意中修改Bean包含的屬性……這樣既可以享用它的便利,又可以防止由此引入的錯(cuò)誤可能!!
3.1. BeanUtils和PropertyUtils
這兩個(gè)類幾乎有一摸一樣的功能,唯一的區(qū)別是:BeanUtils在對(duì)Bean賦值是會(huì)進(jìn)行類型轉(zhuǎn)化。舉例來說也就是在copyProperty時(shí)只要屬性名相同,就算類型不同,BeanUtils也可以進(jìn)行copy;而PropertyBean則可能會(huì)報(bào)錯(cuò)!!
針對(duì)上面的例子,新建一個(gè)Company2的類,其中代碼與Company一樣,只是將otherinfo從String[]改為String。
Company c = init();
Company
BeanUtils.copyProperties(c2,c);
// PropertyUtils.copyProperties(c2,c); 這句會(huì)報(bào)錯(cuò)!!
System.out.println(c2.getOtherInfo());
當(dāng)然2個(gè)Bean之間的同名屬性的類型必須是可以轉(zhuǎn)化的,否則用BeanUtils一樣會(huì)報(bào)錯(cuò)。
若實(shí)現(xiàn)了org.apache.commons.beanutils.Converter接口則可以自定義類型之間的轉(zhuǎn)化。
由于不做類型轉(zhuǎn)化,用PropertyUtils在速度上會(huì)有很大提高!
此外,不作類型轉(zhuǎn)化還有一個(gè)好處,如下面的代碼:
//test data type convert
// ArrayList a1 = BeanUtils.getProperty(c,"product"); //BeanUtils返回的是String
System.out.println("--" + BeanUtils.getProperty(c,"product")); //取出后直接被轉(zhuǎn)為String
ArrayList a = (ArrayList)PropertyUtils.getProperty(c,"product");//PropertyUtils返回的是Object
System.out.println("--" + a.get(1));
用BeanUtils無法返回一個(gè)對(duì)象(除非自己寫一個(gè)Converter),它會(huì)自動(dòng)進(jìn)行類型轉(zhuǎn)化,然后返回String。對(duì)于想返回java類或自定義類的話,還是不要老它大駕轉(zhuǎn)化了。
所有的XXXUtils類都提供的是靜態(tài)方法,可以直接調(diào)用,其主要實(shí)現(xiàn)都在相應(yīng)的XXXUtilsBean中:
BeanUtils ——> BeanUtilsBean
ConvertUtils ——> ConvertUtilsBean
PropertyUtils ——> PropertyUtilsBean
其意思看類名也應(yīng)該知道的差不多了,我就不再廢話啦!當(dāng)然你也可以直接調(diào)用那些XXXUtilsBean,功能都一樣!