详解Apache的对象复制实例教程

零下一度
零下一度 原创
2017-06-30 09:55:01 1311浏览

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 }

需求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 }

以上就是详解Apache的对象复制实例教程的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。