天天看點

BeanUtils.copyProperties() 用法 Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cg

最近在項目中接觸到了BeanUtils.copyProperties(),之前沒怎麼用過這個方法,在網上搜尋了一下,發現

一個Spring的

一個Apache的

Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cg

: http://www.open-open.com/lib/view/open1404998048200.html

BeanUtils.copyProperties() 用法 Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cg

還有一個PropertyUtils.copyProperties()的方法,下面對這兩個方法做一下總結。另外,在使用的時候,程式抛出了異常,經過分析得知,異常的原因是因為指派的對象中有一個時間類型(該屬相采用java.util.Date的類型),程式無法通過,下面主要分析這兩個問題。

BeanUtils.copyProperties() 用法 Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cg

方法/步驟

  1. 第一步: BeanUtils.copyProperties()與PropertyUtils.copyProperties()

    1、 通過反射将一個對象的值指派個另外一個對象(前提是對象中屬性的名字相同)。

    2、 BeanUtils.copyProperties(obj1,obj2); 經常鬧混不知道是誰給誰指派,無意中先到"後付前"這個詞來幫助自己記憶這個功能。即将obj2的值指派給obj1。

    3、 如果2中執行個體obj2為空對象,即值new了他的執行個體并沒有指派的話obj1對應的屬性值也會被設定為空置。

    4、BeanUtils與PropertyUtils對比(這裡對比copyProperties方法)

    PropertyUtils的copyProperties()方法幾乎與BeanUtils.copyProperties()相同,主要的差別在于後者提供類型轉換功能,即發現兩個JavaBean的同名屬性為不同類型時,在支援的資料類型範圍内進行轉換,BeanUtils 不支援這個功能,但是BeanUtils速度會更快一些。

    主要支援轉換類型如下:

    * 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

    不支援java.util.Date轉換,但支援java.sql.Date。如果開發中Date類型采用util而非sql.Date程式會抛出argument mistype異常。

  2. 第二步:擴充BeanUtils支援時間類型轉換

    import java.lang.reflect.InvocationTargetException;

    import org.apache.commons.beanutils.BeanUtils;

    import org.apache.commons.beanutils.ConvertUtils;

    public class BeanUtilsExtends extends BeanUtils {

       static {

           ConvertUtils.register(new DateConvert(), java.util.Date.class);

           ConvertUtils.register(new DateConvert(), java.sql.Date.class);

       }

       public static void copyProperties(Object dest, Object orig) {

           try {

               BeanUtils.copyProperties(dest, orig);

           } catch (IllegalAccessException ex) {

               ex.printStackTrace();

           } catch (InvocationTargetException ex) {

               ex.printStackTrace();

           }

       }

    }

    import java.text.ParseException;

    import java.text.SimpleDateFormat;

    import org.apache.commons.beanutils.Converter;

    public class DateConvert implements Converter {

       public Object convert(Class arg0, Object arg1) {

           String p = (String) arg1;

           if (p == null || p.trim().length() == 0) {

               return null;

           }

           try {

               SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

               return df.parse(p.trim());

           } catch (Exception e) {

               try {

                   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

                   return df.parse(p.trim());

               } catch (ParseException ex) {

                   return null;

               }

           }

       }

    }

  3. 源碼如下:
  4. public static void copyProperties(Object source, Object target) throws BeansException {
    		copyProperties(source, target, null, null);
    	}
    
    	/**
    	 * Copy the property values of the given source bean into the given target bean,
    	 * only setting properties defined in the given "editable" class (or interface).
    	 * <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
    	 * @param editable the class (or interface) to restrict property setting to
    	 * @throws BeansException if the copying failed
    	 * @see BeanWrapper
    	 */