Apache的對象複製詳解,apache對象詳解

來源:互聯網
上載者:User

Apache的對象複製詳解,apache對象詳解

BeanUtils.copyProperties 和 PropertyUtils.copyProperties 

兩個工具類都是對兩個bean之前存在name相同的屬性進行處理,無論是源bean或者目標bean多出的屬性均不處理。

其原理是通過JDK內建的反射機制動態去get,set,從而去轉換我們的類。

但是要注意一點他們所支援的資料類型,還有一個就是假如一個類裡面又寫了一個類,一般叫做內部類,像這種類進行轉換的時候,是不會成功的。

兩者最大的區別是: 
1.BeanUtils.copyProperties會進行類型轉換,而PropertyUtils.copyProperties不會。 
既然進行了類型轉換,那BeanUtils.copyProperties的速度比不上PropertyUtils.copyProperties。 
因此,PropertyUtils.copyProperties應用的範圍稍為窄一點,它只對名字和類型都一樣的屬性進行copy,如果名字一樣但類型不一樣,它會報錯。

 2.對null的處理:PropertyUtils支援為null的情境;BeanUtils對部分屬性不支援null的情況,具體為下:

1)、date類型不支援;
2)、Boolean、Ineger、Long、Short、Float、Double等不支援: 轉為false、0;
3)、string:支援,保持null;

使用BeanUtils有幾個要注意的地方: 
1.對於類型為Boolean/Short/Integer/Float/Double的屬性,它會轉換為false、0: 

 1 public class User {   2    3     private Integer intVal;   4        5     private Double doubleVal;   6        7     private Short shortVal;   8        9     private Long longVal;  10       11     private Float floatVal;  12       13     private Byte byteVal;  14       15     private Boolean booleanVal;  16 }  17   18 User src = new User();  19 User dest = new User();  20 BeanUtils.copyProperties(dest, src);  21 System.out.println(src);  22 System.out.println(dest);  23   24 //輸出結果:      25 User [intVal=null, doubleVal=null, shortVal=null, longVal=null, floatVal=null, byteVal=null, booleanVal=null]  26 User [intVal=0, doubleVal=0.0, shortVal=0, longVal=0, floatVal=0.0, byteVal=0, booleanVal=false]  
View Code

解釋說是因為這幾個類型都有對應的基本類型,在進行類型轉換時,有可能遇到類似Integer -> int的轉換,此時顯然不能對int類型的屬性賦值為null,因此統一轉換為0。 

如何讓它不要轉為0呢?可以這樣:

1 import org.apache.commons.beanutils.converters.IntegerConverter;  2   3 IntegerConverter converter = new IntegerConverter(null);    //預設為null,而不是0  4 BeanUtilsBean beanUtilsBean = new BeanUtilsBean();  5 beanUtilsBean.getConvertUtils().register(converter, Integer.class);
View Code

2.對於java.util.Date/BigDecimal/java.sql.Date/java.sql.Timestamp/java.sql.Time這幾個類,如果值為null,則在copy時會拋異常,需要使用對應的Conveter: 

 1 public class User2 {   2    3     private java.util.Date javaUtilDateVal;   4        5     private java.sql.Date javaSqlDateVal;   6        7     private java.sql.Timestamp javaSqlTimeStampVal;   8        9     private BigDecimal bigDecimalVal;  10   11     private java.sql.Time javaSqlTime;  12   13 }  14   15 User2 src = new User2();  16 User2 dest = new User2();  17   18 BeanUtilsBean beanUtilsBean = new BeanUtilsBean();  19   20 //如果沒有下面幾行,則在轉換null時會拋異常,例如:org.apache.commons.beanutils.ConversionException: No value specified for 'BigDecimal'  21 //在org.apache.commons.beanutils.converters這個包下面有很多的Converter,可以按需要使用  22 beanUtilsBean.getConvertUtils().register(new BigDecimalConverter(null), BigDecimal.class);  23 beanUtilsBean.getConvertUtils().register(new DateConverter(null), java.util.Date.class);  24   25 beanUtilsBean.getConvertUtils().register(new SqlTimestampConverter(null), java.sql.Timestamp.class);  26 beanUtilsBean.getConvertUtils().register(new SqlDateConverter(null), java.sql.Date.class);  27 beanUtilsBean.getConvertUtils().register(new SqlTimeConverter(null), java.sql.Time.class);  28   29 beanUtilsBean.copyProperties(dest, src);  30 System.out.println(src);  31 System.out.println(dest);  
View Code

假設是從A複製到B: 
需求1:如果B中某欄位有值(不為null),則該欄位不複製;也就是B中該欄位沒值時,才進行複製,適合於對B進行補儲值的情況。

 1 import org.apache.commons.beanutils.BeanUtilsBean;   2 import org.apache.commons.beanutils.PropertyUtils;   3    4 public class CopyWhenNullBeanUtilsBean extends BeanUtilsBean{   5    6     @Override   7     public void copyProperty(Object bean, String name, Object value)   8             throws IllegalAccessException, InvocationTargetException {   9         try {  10             Object destValue = PropertyUtils.getSimpleProperty(bean, name);  11             if (destValue == null) {  12                 super.copyProperty(bean, name, value);  13             }  14         } catch (NoSuchMethodException e) {  15             throw new RuntimeException(e);  16         }  17     }  18   19 }  
View Code

需求2:如果A中某欄位沒值(為null),則該欄位不複製,也就是不要把null複製到B當中。

 1 import org.apache.commons.beanutils.BeanUtilsBean;   2    3 public class CopyFromNotNullBeanUtilsBean extends BeanUtilsBean {   4    5     @Override   6     public void copyProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException {   7         if (value == null) {   8             return;   9         }  10         super.copyProperty(bean, name, value);  11     }  12 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.