雖然目前已經有很多文章介紹此方法的用法,但是既然自己遇到了。 還是記錄一下吧。
導包:import org.sPRingframework.beans.BeanUtils;
源代碼的介紹:
/** * Copy the property values of the given source bean into the target bean. * <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, * consider using a full BeanWrapper. * @param source the source bean * @param target the target bean * @throws BeansException if the copying failed * @see BeanWrapper */ public static void copyProperties(Object source, Object target) throws BeansException { copyProperties(source, target, null, (String[]) null); }介紹: BeanUtils.copyProperties(bean, detailBean); //bean 給detailBean 賦值 此方法是將前面的bean 給detailBean賦值。
詳細介紹: BorrowInfoBean bean=new BorrowInfoBean(); bean=this.getBorrowInfo(999); //此句意思就是查詢bean的信息;
BorrowInfoBean detailBean=new BorrowInfoBean (); //新的bean
BeanUtils.copyProperties(bean, detailBean); //bean 給detailBean 賦值
(未驗證,從別的博客看到)要注意一點,java.util.Date是不被支持的,而它的子類java.sql.Date是被支持的。因此如果對象包含時間類型的屬性,且希望被轉換的時候,一定要使用java.sql.Date類型。否則在轉換時會提示argument mistype異常。
新聞熱點
疑難解答