Details about object replication in Apache and details about apache objects
BeanUtils. copyProperties and PropertyUtils. copyProperties
Both tool classes process attributes with the same name before the two beans. No extra attributes are processed for the source bean or target bean.
The principle is to use the built-in reflection mechanism of JDK to dynamically get and set to convert our classes.
But pay attention to the data types they support. The other one is that if another class is written in a class, it is generally called an internal class. When this type is converted, it won't succeed.
The biggest difference between the two is:
1. BeanUtils. copyProperties will convert the type, but PropertyUtils. copyProperties will not.
Since data type conversion is performed, BeanUtils. copyProperties is not as fast as PropertyUtils. copyProperties.
Therefore, the range of the PropertyUtils. copyProperties application is a little narrower. It only copies attributes of the same name and type. If the name is the same but the type is different, an error is returned.
2. Processing of null: PropertyUtils supports null scenarios; BeanUtils does not support null for some attributes, as shown below:
1) date type is not supported;
2 ),Boolean,Ineger, Long, Short, Float, Double, etc. are not supported: Convert to false, 0;
3), string: supported, keep null;
Note the following when using BeanUtils:
1. For properties of the type Boolean/Short/Integer/Float/Double, it is converted to 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 // output result: 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
It is explained that these types have their corresponding basic types. During type conversion, you may encounter conversions similar to Integer-> int, in this case, the int type attribute cannot be assigned null, so the value is uniformly converted to 0.
How can we avoid converting it to 0? You can do this:
1 import org. apache. commons. beanutils. converters. integerConverter; 2 3 IntegerConverter converter = new IntegerConverter (null); // The default value is null, instead of 0 4 BeanUtilsBean beanUtilsBean = new BeanUtilsBean (); 5 beanUtilsBean. getConvertUtils (). register (converter, Integer. class );
View Code
2. for java. util. date/BigDecimal/java. SQL. date/java. SQL. timestamp/java. SQL. if the value is null, an exception is thrown during copy. The corresponding Conveter must be used:
1 public class User2 {2 3 private java. util. date javaUtilDateVal; 4 5 private java. SQL. date 1_qldateval; 6 7 private java. SQL. timestamp implements qltimestampval; 8 9 private BigDecimal bigDecimalVal; 10 11 private java. SQL. time required qltime; 12 13} 14 15 User2 src = new User2 (); 16 User2 dest = new User2 (); 17 18 BeanUtilsBean beanUtilsBean = new BeanUtilsBean (); 19 20 // if there are no rows below, an exception will be thrown when converting null, for example: org. apache. commons. beanutils. conversionException: No value specified for 'bigdecimal '21 // At org. apache. commons. beanutils. the converters package contains many converters. You can use 22 beanUtilsBean as needed. 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
Assume that the data is copied from A to B:
Requirement 1: If a field in B has a value (not null), the field is not copied; that is, the field in B has no value, it is suitable for recharging 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
Requirement 2: If A field in A has no value (null), the field is not copied, that is, do not copy null to 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