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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM