BeanUtils.copyProperties()所有的空值不复制问题

目录
  • BeanUtils.copyProperties()所有的空值不复制
    • 第一种情况
    • 第二种情况
  • BeanUtils.copyProperties()的用法和注意点
    • 属性为null也会被复制,内部类不会复制过去
    • 注意点一
    • 注意点二

BeanUtils.copyProperties()所有的空值不复制

第一种情况

所有为空值的属性都不copy

直接上代码吧~

public class UpdateUtil {
    /**
     * 所有为空值的属性都不copy
     *
     * @param source
     * @param target
     */
    public static void copyNullProperties(Object source, Object target) {
        BeanUtils.copyProperties(source, target, getNullField(source));
    }
    /**
     * 获取属性中为空的字段
     *
     * @param target
     * @return
     */
    private static String[] getNullField(Object target) {
        BeanWrapper beanWrapper = new BeanWrapperImpl(target);
        PropertyDescriptor[] propertyDescriptors = beanWrapper.getPropertyDescriptors();
        Set<String> notNullFieldSet = new HashSet<>();
        if (propertyDescriptors.length > 0) {
            for (PropertyDescriptor p : propertyDescriptors) {
                String name = p.getName();
                Object value = beanWrapper.getPropertyValue(name);
                if (Objects.isNull(value)) {
                    notNullFieldSet.add(name);
                }
            }
        }
        String[] notNullField = new String[notNullFieldSet.size()];
        return notNullFieldSet.toArray(notNullField);
    }
    public static void main(String[] args) {
        TopMenuConfigEntity topMenuConfigEntity1 = new TopMenuConfigEntity();
        topMenuConfigEntity1.setWardCode("cat");
        topMenuConfigEntity1.setTitle("animal");
        TopMenuConfigEntity topMenuConfigEntity2 = new TopMenuConfigEntity();
        topMenuConfigEntity2.setWardCode("dog");
        UpdateUtil.copyNullProperties(topMenuConfigEntity2,topMenuConfigEntity1);
        System.out.println(topMenuConfigEntity1.getTitle());
    }
}

执行main 方法后,topMenuConfigEntity1的title还是为原来的“animal”值,没有被topMenuConfigEntity2 的空值覆盖。

第二种情况

原对象的属性有值,复制时指定某些字段不复制

调BeanUtils的这个方法

public static void copyProperties(Object source, Object target, String... ignoreProperties) throws BeansException {
        copyProperties(source, target, null, ignoreProperties);
    }
 public static void main(String[] args) {
        TopMenuConfigEntity topMenuConfigEntity1 = new TopMenuConfigEntity();
        topMenuConfigEntity1.setWardCode("cat");
        topMenuConfigEntity1.setTitle("animal");
        topMenuConfigEntity1.setCreateTime(new Date());
        TopMenuConfigEntity topMenuConfigEntity2 = new TopMenuConfigEntity();
        String[] ignoreArray = new String[]{"title","createTime"};
        BeanUtils.copyProperties(topMenuConfigEntity2,topMenuConfigEntity1,ignoreArray);
        System.out.println("title : "+topMenuConfigEntity2.getTitle() +";createTime :" + topMenuConfigEntity2.getCreateTime());
    }

topMenuConfigEntity2的title 和createTime为null,没有复制

BeanUtils.copyProperties()的用法和注意点

属性为null也会被复制,内部类不会复制过去

BeanUtils提供对Java反射和自省API的包装。其主要目的是利用反射机制对JavaBean的属性进行处理。我们知道,一个JavaBean通常包含了大量的属性,很多情况下,对JavaBean的处理导致大量get/set代码堆积,增加了代码长度和阅读代码的难度。

BeanUtils是这个包里比较常用的一个工具类,这里只介绍它的copyProperties()方法。

该方法源码如下:

public static void copyProperties(Object source, Object target) throws BeansException {
        copyProperties(source, target, (Class)null, (String[])null);
    }

public static void copyProperties(Object source, Object target, Class<?> editable) throws BeansException {
        copyProperties(source, target, editable, (String[])null);
    }

public static void copyProperties(Object source, Object target, String... ignoreProperties) throws BeansException {
        copyProperties(source, target, (Class)null, ignoreProperties);
    }

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;
        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 && (ignoreList == 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, new Object[0]);
                            if(!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }

                            writeMethod.invoke(target, new Object[]{value});
                        } catch (Throwable var15) {
                            throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", var15);
                        }
                    }
                }
            }
        }

    }

如果你有两个具有很多相同属性的JavaBean,就可以试用该方法将sourse中的属性copy到target中,如果sourse和target间存在名称不相同的属性,则BeanUtils不对这些属性进行处理,需要程序员手动处理。

怎么样,很方便吧!除BeanUtils外还有一个名为PropertyUtils的工具类,它也提供copyProperties()方法,作用与 BeanUtils的同名方法十分相似,主要的区别在于后者提供类型转换功能,即发现两个JavaBean的同名属性为不同类型时,在支持的数据类型范围内进行转换,而前者不支持这个功能,但是速度会更快一些。

BeanUtils支持的转换类型如下:

* java.lang.BigDecimal   

* java.lang.BigInteger   

* boolean and java.lang.Boolean   

* byte and java.lang.Byte   

* char and java.lang.Character   

* java.lang.Class   

* double and java.lang.Double   

* float and java.lang.Float   

* int and java.lang.Integer   

* long and java.lang.Long   

* short and java.lang.Short   

* java.lang.String   

* java.sql.Date   

* java.sql.Time   

* java.sql.Timestamp

这里要注意一点,java.util.Date是不被支持的,而它的子类java.sql.Date是被支持的。因此如果对象包含时间类型的属性,且希望被转换的时候,一定要使用java.sql.Date类型。否则在转换时会提示argument mistype异常。

现在,还有一个坏消息:使用BeanUtils的成本惊人地昂贵!我做了一个简单的测试,BeanUtils所花费的时间要超过取数 据、将其复制到对应的 value对象(通过手动调用get和set方法),以及通过串行化将其返回到远程的客户机的时间总和。所以要小心使用。

注意点一

apache和spring的工具包中都有BeanUtils,使用其中的copyProperties方法可以非常方便的进行这些工作,但在实际应用中发现,对于null的处理不太符合个人的需要,例如在进行修改操作中只需要对model中某一项进行修改,那么一般我们在页面上只提交model的ID及需要修改项的值,这个时候使用BeanUtils.copyProperties会将其他的null绑定到pojo中去。

大家可以直接调用我们加工类的copyPropertiesIgnoreNull()方法即可忽略null值,避免老数据被null覆盖的尴尬。具体代码如下:

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import java.util.HashSet;
import java.util.Set; 

public class SpringUtil implements ApplicationContextAware {

    /**
     * 当前IOC
     *
     */
    private static ApplicationContext applicationContext;  

    /**
     * * 设置当前上下文环境,此方法由spring自动装配
     *
     */
    @Override
    public void setApplicationContext(ApplicationContext arg0)
            throws BeansException {
        applicationContext = arg0;
    }  

    /**
     * 从当前IOC获取bean
     *
     * @param id
     * bean的id
     * @return
     *
     */
    public static Object getObject(String id) {
        Object object = null;
        object = applicationContext.getBean(id);
        return object;
    } 

    public static String[] getNullPropertyNames (Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for(java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    public static void copyPropertiesIgnoreNull(Object src, Object target){
        BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
    }
}

调用:copyPropertiesIgnoreNull

public class TestBeanUtiles {
    public static void main(String[] args) {
        NewPerson newPerson = new NewPerson();
        newPerson.setName("bifuguo");//前台用户更新过的数据,例如前台只修改了用户名
        //下面我们假设是从数据库加载出来的老数据
        OldPerson oldPerson = new OldPerson();
        oldPerson.setSex("nv");
        oldPerson.setAge(5);
        //如果我们想把新数据更新到老数据这个对象里面,我们就可以借助BeanUtils.copyProperties()的方法如下:
        //BeanUtils.copyProperties(newPerson, oldPerson);
        SpringUtil.copyPropertiesIgnoreNull(newPerson, oldPerson);
        System.out.println(newPerson.toString());
        System.out.println(oldPerson.toString());
    }
}

打印结果:

NewPerson{name='bifuguo', sex='null', age=0}
OldPerson{name='bifuguo', sex='nv', age=0}

现在就可以看出老数据没有被null覆盖

注意点二

1.Spring的BeanUtils的CopyProperties方法需要对应的属性有getter和setter方法;

2.如果存在属性完全相同的内部类,但是不是同一个内部类,即分别属于各自的内部类,则spring会认为属性不同,不会copy;

3.泛型只在编译期起作用,不能依靠泛型来做运行期的限制;

4.最后,spring和apache的copy属性的方法源和目的参数的位置正好相反,所以导包和调用的时候都要注意一下。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • BeanUtils.copyProperties在拷贝属性时忽略空值的操作

    BeanUtils.copyProperties忽略空值 使用spring开发的人,对这行代码肯定不陌生,常用于DTO.VO.PO之间的复制. /** * 全属性copy对象 * **/ BeanUtils.copyProperties(Object source, Object target) 但这行代码会将所有的属性都进行copy,有的时候我们想要个别属性不进行复制(比如:null值属性),这时就需要用到另一个方法: /** * 忽略某些属性copy对象 * **/ BeanUtils.co

  • BeanUtils.copyProperties复制对象结果为空的原因分析

    目录 BeanUtils.copyProperties复制对象结果为空原因 正确的包 错误的包 两个类的区别 BeanUtils.copyProperties拷贝没成功的坑,记录解决原因 具体如下 BeanUtils.copyProperties复制对象结果为空原因 细心比对,发现原来是导错了包导致的 正确的包 import org.springframework.beans.BeanUtils; 错误的包 import org.apache.commons.beanutils.BeanUtil

  • 解决BeanUtils.copyProperties不支持复制集合的问题

    工作中,经常使用Spring的工具类BeanUtils.copyProperties对bean属性进行复制,这里的复制属于浅复制.且不能复制集合和数组.本文会对该工具进行一些测试. 文末会提出复制集合属性的解决方案 准备工作:准备测试需要的类 @Data public class Class { private People[] member; private People teacher; private List<People> student; } @Data @NoArgsConstr

  • BeanUtils.copyProperties复制属性失败的原因及解决方案

    目录 BeanUtils.copyProperties复制属性失败 描述 解决办法 BeanUtils.copyProperties应用的改进 为解决这个问题我重写了部分spring BeanUtils的代码 BeanUtils.copyProperties复制属性失败 描述 在JavaE中使用 BeanUtils.copyProperties,把A对象的name.age等属性复制到B对象中,A与B对象的类型不同.出现的问题是复制属性失败,根本原因是 BeanUtils找不到set.get方法.

  • BeanUtils.copyProperties复制不生效的解决

    目录 前言 问题的排查 问题的扩展 前言 呵呵 前端时间使用 BeanUtils.copyProperties 的时候碰到了一个这样的问题 我有两个实体, 有同样的属性, 一个有给定的属性的 getter, 另外一个有 给定的属性的 setter, 但是 我使用 BeanUtils.copyProperties 的时候 把来源对象的这个属性 复制不到 目标对象上面 然后 当时也跟踪了一下代码, 然后 这里整理一下 改代码片段吧 然后在调试的过程中 也发现了一些其他的问题, 呵呵 算是额外的了解吧

  • BeanUtils.copyProperties()所有的空值不复制问题

    目录 BeanUtils.copyProperties()所有的空值不复制 第一种情况 第二种情况 BeanUtils.copyProperties()的用法和注意点 属性为null也会被复制,内部类不会复制过去 注意点一 注意点二 BeanUtils.copyProperties()所有的空值不复制 第一种情况 所有为空值的属性都不copy 直接上代码吧~ public class UpdateUtil { /** * 所有为空值的属性都不copy * * @param source * @p

  • 解决Beanutils.copyproperties实体类对象不一致的问题

    今天给大家分析一个解决Beanutils.copyproperties实体类对象名不一致的解决方法,一般我们在两个对象拷贝的问题上,我个人用的比较多的就是Beanutils.copyproperties,字段名如果不一致的话就去实体类中使用重载,把当前实体类的对象赋值给另外一个对象,也有用到set(),当然这些也都能解决Beanutils.copyproperties实体类属性不一致的问题,不过今天要给大家分享的是,不用set()和实体类的重构,使用类的反射机制去完成! 话不多说直接开始: 我是

  • 基于Beanutils.copyProperties()的用法及重写提高效率

    目录 Beanutils.copyProperties()用法及重写提高效率 一.简介 二.用法 三.重写 原理 BeanUtils.copyProperties 使用注意 示例演示 开始进行示例 最后强调 Beanutils.copyProperties()用法及重写提高效率 特别说明本文介绍的是Spring(import org.springframework.beans.BeanUtils)中的BeanUtils.copyProperties(A,B)方法.是将A中的值赋给B.apache

  • BeanUtils.copyProperties()拷贝id属性失败的原因及解决

    目录 BeanUtils.copyProperties()拷贝id属性失败 部分代码如下 解决方法 BeanUtils.copyProperties 出错 BeanUtils.copyProperties()拷贝id属性失败 po类中id有值,但是使用BeanUtils.copyProperties()拷贝出的vo类id属性为null,检查后发现是因为po继承的父类声明了一个泛型. 部分代码如下 public abstract class AbstractEntity<ID extends Se

  • BeanUtils.copyProperties使用总结以及注意事项说明

    目录 1.前言 2.一般使用 3.拷贝属性时忽略空值 4.使用注意事项(1) 5.使用注意事项(2) 6.使用注意事项(3) 1.前言 开发过程中,讲一个对象的属性和值赋值到另一个对象上,大量使用了get.set方法,看着很臃肿,思考下肯定不只有我有这种想法,所以技术上肯定有方法能解决这个问题,所以查阅了一些资料发现了BeanUtils.copyProperties这个方法以下是这次所有的总结以及使用时的注意事项. 使用org.springframework.beans.BeanUtils.co

随机推荐