开发在中经常需要将一个对象的若干个值赋值给另外一个对象相对应的字段,且字段名是一样的,如果一个一个取一个一个赋值
太麻烦,使用org.springframework.beans.BeanUtils
的copyProperties方法,有个弊端就是会将null也复制过来
自己写个工具类,继承org.springframework.beans.BeanUtils
对该方法进行改造,如代码中注释部分
public static void copyProperties(Object source, Object target) throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
Class<?> actualEditable = target.getClass();
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
PropertyDescriptor[] var7 = targetPds;
int var8 = targetPds.length;
for (int var9 = 0; var9 < var8; ++var9) {
PropertyDescriptor targetPd = var7[var9];
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null)