重写org.springframework.beans.BeanUtils的copyProperties(Object source,Object target)方法 从model复制属性到pojo中
2014-06-06 22:16
567 查看
当一个对象中有几十个字段,而我们修改该对象信息时,可能只修改其中的几个或十几个,通过spring的BeanUtils的copyProperties方法将model复制到pojo时,会将model中的null属性也全部复制到了pojo中,这样原来不想修改的属性变为了null,这不是我们想要的。
当然可以通过get从model中取出要修改的属性,然后再set到pojo中,但这样比较麻烦。
所以自己新建一个BeanUtils类,继承org.springframework.beans.BeanUtils类,覆盖copyProperties(Object source, Object target)方法,重写copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties)方法,从model复制属性到pojo中时,model中为null的属性不复制pojo中。
重写代码:
package com.goldweb.utils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* 重写 org.springframework.beans.BeanUtils 的 copyProperties(Object source, Object target) 方法
* 从model复制属性到pojo中时,model中为null的属性不复制pojo中
* @author liangshishen
*
*/
public abstract class BeanUtils extends org.springframework.beans.BeanUtils {
/**
* 从org.springframework.beans.BeanUtils类中直接复制过来
* @param source
* @param target
* @throws BeansException
*/
public static void copyProperties(Object source, Object target) throws BeansException {
copyProperties(source, target, null, (String[]) null);
}
/**
* 从org.springframework.beans.BeanUtils类中直接复制过来,修改部分代码
* @param source
* @param target
* @param editable
* @param ignoreProperties
* @throws BeansException
*/
private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties)
throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
Class<?> actualEditable = target.getClass();
if (editable != null) {
if (!editable.isInstance(target)) {
throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
"] not assignable to Editable class [" + editable.getName() + "]");
}
actualEditable = editable;
}
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;
for (PropertyDescriptor targetPd : targetPds) {
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) {
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null) {
Method readMethod = sourcePd.getReadMethod();
if (readMethod != null &&
ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
// 判断被复制的属性是否为null, 如果不为null才复制
if (value != null) {
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
}
catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
}
}
}
}
使用:
public boolean updateUser(UserModel userModel) {
if (userModel != null) {
User user = new User();
// 为了不与spring的BeanUtils冲突, 这里用全名调用自定义的BeanUtils
// 这样userModel中为null的属性将不会复制到user中, user中没有修改的属性还保持原来的, 不会被复制为null了
com.goldweb.utils.BeanUtils.copyProperties(userModel, user);
// 更新用户
userDAO.merge(user);
return true;
}
return false;
}
当然可以通过get从model中取出要修改的属性,然后再set到pojo中,但这样比较麻烦。
所以自己新建一个BeanUtils类,继承org.springframework.beans.BeanUtils类,覆盖copyProperties(Object source, Object target)方法,重写copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties)方法,从model复制属性到pojo中时,model中为null的属性不复制pojo中。
重写代码:
package com.goldweb.utils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* 重写 org.springframework.beans.BeanUtils 的 copyProperties(Object source, Object target) 方法
* 从model复制属性到pojo中时,model中为null的属性不复制pojo中
* @author liangshishen
*
*/
public abstract class BeanUtils extends org.springframework.beans.BeanUtils {
/**
* 从org.springframework.beans.BeanUtils类中直接复制过来
* @param source
* @param target
* @throws BeansException
*/
public static void copyProperties(Object source, Object target) throws BeansException {
copyProperties(source, target, null, (String[]) null);
}
/**
* 从org.springframework.beans.BeanUtils类中直接复制过来,修改部分代码
* @param source
* @param target
* @param editable
* @param ignoreProperties
* @throws BeansException
*/
private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties)
throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
Class<?> actualEditable = target.getClass();
if (editable != null) {
if (!editable.isInstance(target)) {
throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
"] not assignable to Editable class [" + editable.getName() + "]");
}
actualEditable = editable;
}
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;
for (PropertyDescriptor targetPd : targetPds) {
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) {
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null) {
Method readMethod = sourcePd.getReadMethod();
if (readMethod != null &&
ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
// 判断被复制的属性是否为null, 如果不为null才复制
if (value != null) {
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
}
catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
}
}
}
}
使用:
public boolean updateUser(UserModel userModel) {
if (userModel != null) {
User user = new User();
// 为了不与spring的BeanUtils冲突, 这里用全名调用自定义的BeanUtils
// 这样userModel中为null的属性将不会复制到user中, user中没有修改的属性还保持原来的, 不会被复制为null了
com.goldweb.utils.BeanUtils.copyProperties(userModel, user);
// 更新用户
userDAO.merge(user);
return true;
}
return false;
}
相关文章推荐
- org.springframework.beans.BeanUtils.copyProperties方法的泛型问题。
- java 使用BeanUtils.copyProperties(Object source,Object target) 复制字段值
- org.springframework.beans.Beanutils.copyProperties 原理
- 使用Spring提供的BeanUtils.copyProperties()方法报错:Could not copy property 'xxx' from source to target
- org.springframework.beans.Beanutils copyProperties 原理
- 重写BeanUtils.copyProperties(复制对象属性方法)
- (复制对象属性方法)beanutils.copyproperties
- java.lang.ClassNotFoundException: org.springframework.aop.TargetSource
- 【exception】java.lang.NoSuchMethodError: org.springframework.beans.Beans.BeanUtils
- org.apache.commons.beanutils.copyProperties方法简介
- java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name的解决方法
- Spring 的BeanUtils.copyProperties在拷贝属性时忽略空值
- org.springframework.beans.BeanUtils
- nested exception is org.springframework.beans.ConversionNotSupportedException的解决方法
- Caused by: java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
- BeanUtils.copyProperties(source, target)用法
- org.apache.commons.beanutils.copyProperties中复制不成功但是不报错问题
- bean之间的复制!BeanUtils.copyProperties、set、BeanCopier还有spring中的BeanUtils.copyProperties之间的区别
- org.springframework.beans.factory.config.PropertiesFactoryBean