一邦危、前言
BeanUtils提供對(duì)Java反射和自省API的包裝洋侨。其主要目的是利用反射機(jī)制對(duì)JavaBean的屬性進(jìn)行處理。我們知道倦蚪,一個(gè)JavaBean通常包含了大量的屬性希坚,很多情況下,對(duì)JavaBean的處理導(dǎo)致大量get/set代碼堆積陵且,增加了代碼長(zhǎng)度和閱讀代碼的難度裁僧。
二、用法:
BeanUtils是這個(gè)包里比較常用的一個(gè)工具類慕购,這里只介紹它的copyProperties()方法聊疲。該方法定義如下(在這里我繼承了自帶的BeanUtils重寫(xiě)他的copyProperties方法):
public class BeanUtil extends org.springframework.beans.BeanUtils {
/**
* Copy the property values of the given source bean into the target class.
* <p>Note: The source and target classes do not have to match or even be derived
* from each other, as long as the properties match. Any bean properties that the
* source bean exposes but the target bean does not will silently be ignored.
* <p>This is just a convenience method. For more complex transfer needs,
*
* @param source the source bean
* @param target the target bean class
* @param <T> 泛型標(biāo)記
* @return T
* @throws BeansException if the copying failed
*/
public static <T> T copyProperties(Object source, Class<T> target) throws BeansException {
T to = newInstance(target);
BeanUtil.copyProperties(source, to);
return to;
}
}
如果你有兩個(gè)具有很多相同屬性的JavaBean,一個(gè)很常見(jiàn)的情況就是實(shí)體類對(duì)象(持久對(duì)象)和對(duì)應(yīng)的數(shù)據(jù)傳輸對(duì)象沪悲,例如 User和UserForm获洲。我們一般會(huì)在Action里從ActionForm構(gòu)造一個(gè)PO對(duì)象,傳統(tǒng)的方式是使用類似下面的語(yǔ)句對(duì)屬性逐個(gè)賦值:
//得到UserForm
UserForm userForm=(UserForm)form;
//構(gòu)造User對(duì)象
User user=new User();
//賦值
user.setName(userForm.getName());
user.setAge(userForm.getAge());
user.setGender(userForm.getGender());
user.setMajor(userForm.getMajor());
user.setDepartment(userForm.getDepartment());
//持久化User對(duì)象到數(shù)據(jù)庫(kù)
HibernateDAO.save(user);
而使用BeanUtils后殿如,代碼就大大改觀了贡珊,如下所示:
//得到UserForm
UserForm userForm =(UserForm)form;
//構(gòu)造Teacher對(duì)象
User user=new User ();
//賦值
BeanUtils.copyProperties(user,userForm);
//持久化Teacher對(duì)象到數(shù)據(jù)庫(kù)
HibernateDAO.save(user);
如果User和UserForm間存在名稱不相同的屬性最爬,則BeanUtils不對(duì)這些屬性進(jìn)行處理,需要程序員手動(dòng)處理飞崖。例如 User包含createDate(該屬性記錄最后修改日期,不需要用戶在界面中輸入)屬性而UserForm無(wú)此屬性谨胞,那么在上面代碼的 copyProperties()后還要加上一句:
teacher.setCreateDate(new Date());
怎么樣固歪,很方便吧!除BeanUtils外還有一個(gè)名為PropertyUtils的工具類胯努,它也提供copyProperties()方法牢裳,作用與 BeanUtils的同名方法十分相似,主要的區(qū)別在于后者提供類型轉(zhuǎn)換功能叶沛,即發(fā)現(xiàn)兩個(gè)JavaBean的同名屬性為不同類型時(shí)蒲讯,在支持的數(shù)據(jù)類型范圍內(nèi)進(jìn)行轉(zhuǎn)換,而前者不支持這個(gè)功能灰署,但是速度會(huì)更快一些判帮。BeanUtils支持的轉(zhuǎn)換類型如下:
* java.lang.BigDecimal
* java.lang.BigInteger
* boolean and java.lang.Boolean
* byte and java.lang.Byte
* char and java.lang.Character
* java.lang.Class
* double and java.lang.Double
* float and java.lang.Float
* int and java.lang.Integer
* long and java.lang.Long
* short and java.lang.Short
* java.lang.String
* java.sql.Date
* java.sql.Time
* java.sql.Timestamp
這里要注意一點(diǎn),java.util.Date是不被支持的溉箕,而它的子類java.sql.Date是被支持的晦墙。因此如果對(duì)象包含時(shí)間類型的屬性,且希望被轉(zhuǎn)換的時(shí)候肴茄,一定要使用java.sql.Date類型晌畅。否則在轉(zhuǎn)換時(shí)會(huì)提示argument mistype異常。
三寡痰、結(jié)論:
我曾在許多不同的項(xiàng)目上或直接或間接地使用各種流行的commons組件抗楔。其中的一個(gè)強(qiáng)大的組件就是BeanUtils。我 將說(shuō)明如何使用BeanUtils將local實(shí)體bean轉(zhuǎn)換為對(duì)應(yīng)的value 對(duì)象:
BeanUtils.copyProperties(aValue, aLocal)
上面的代碼從aLocal對(duì)象復(fù)制屬性到aValue對(duì)象拦坠。它相當(dāng)簡(jiǎn)單连躏!它不管local(或?qū)?yīng)的value)對(duì)象有多少個(gè)屬性,只管進(jìn)行復(fù)制贞滨。我們假設(shè) local對(duì)象有100個(gè)屬性反粥。上面的代碼使我們可以無(wú)需鍵入至少100行的冗長(zhǎng)、容易出錯(cuò)和反復(fù)的get和set方法調(diào)用疲迂。太有用 了才顿!