BeanUtils工具类copyProperties方法缺点及解决

2019-10-28 06:28:34来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

BeanUtils工具类copyProperties方法缺点及解决



使用类为spring-beans:4.3.13release包中的 org.springframework.beans.BeanUtils

BeanUtils.copyProperties(Object source, Object target);
该方法会将source中同名属性的值赋值给target中的同名属性。(前提是source中的改属性类型属于target中该属性类型【即为相同类或其子类,否则目标属性还是为原默认值】
import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.junit.Test;
import org.springframework.beans.BeanUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author
 * @date 2019/10/26
 */
public class BeanUtil {
    public static void main(String[] args) {
        Bean2 bean2 = new Bean2();
        bean2.setIntField(1);
        bean2.setStrField("str");
        bean2.setObjField("obj");
        bean2.setListIntegerField(Arrays.asList("1","2assdsdsf","3"));
        bean2.setObjField1("1");
        Bean1 bean1 = new Bean1();

        //属性中有元素不满足目标属性时依旧赋值成功Bean1.listIntegerField),未对能强转成功的属性做赋值处理(Bean1.objField1)
        BeanUtils.copyProperties(bean2,bean1);

        System.out.println("bean2:\t"+bean2);
        System.out.println("bean1:\t"+bean1);
        System.out.println("bean1.getListIntegerField:\t"+bean1.getListIntegerField().get(0));//调用到具体异常元素再抛出异常

        System.out.println("--------------------分割线--------------------");

        Bean1 bean11 = new Bean1();
        bean11.setIntField(1);
        bean11.setStrField("str");
        bean11.setObjField("obj");
        bean11.setListIntegerField(Arrays.asList(1,2,3));
        bean11.setObjField1(1);
        Bean2 bean21 = new Bean2();

        //Bean2.objField1 类型大于Bean1.objField1类型,赋值成功
        BeanUtils.copyProperties(bean11,bean21);

        System.out.println("bean11:\t"+bean11);
        System.out.println("bean21:\t"+bean21);
        System.out.println("bean21.getListIntegerField:\t"+bean21.getListIntegerField());

   
/*
运行结果:

bean2: BeanUtil.Bean2(intField=1, strField=str, objField=obj, objField1=1, listIntegerField=[1, 2assdsdsf, 3])
bean1: BeanUtil.Bean1(intField=1, strField=str, objField=obj, objField1=null, listIntegerField=[1, 2assdsdsf, 3])
bean1.getListIntegerField: 1
--------------------分割线--------------------
bean11: BeanUtil.Bean1(intField=1, strField=str, objField=obj, objField1=1, listIntegerField=[1, 2, 3])
bean21: BeanUtil.Bean2(intField=1, strField=str, objField=obj, objField1=1, listIntegerField=[1, 2, 3])
bean21.getListIntegerField: [1, 2, 3]

*/
    }

    @Test
    public void optimizeCopyProperties(){
        Bean2 bean2 = new Bean2();
        bean2.setIntField(1);
        bean2.setStrField("str");
        bean2.setObjField("obj");
//        bean2.setListIntegerField(Arrays.asList("1assdsdsf","2","3"));
        bean2.setListIntegerField(Arrays.asList("1","2","3"));
        bean2.setObjField1("1");

        //借助json工具类,底层以字符串实现强转。对能强转成功的属性做赋值并赋值成功,若同名属性值强转赋值失败则抛出异常
        Bean1 bean1 = JSON.parseObject(JSON.toJSONBytes(bean2), Bean1.class);

        System.out.println("bean2:\t"+bean2);
        System.out.println("bean1:\t"+bean1);
        System.out.println("bean1.getListIntegerField:\t"+bean1.getListIntegerField());

/*  运行结果:
bean2: BeanUtil.Bean2(intField=1, strField=str, objField=obj, objField1=1, listIntegerField=[1, 2, 3])
bean1: BeanUtil.Bean1(intField=1, strField=str, objField=obj, objField1=1, listIntegerField=[1, 2, 3])
bean1.getListIntegerField: [1, 2, 3]
*/
} 
   @Data
public static class Bean1{
private int intField;
private String strField;
private Object objField;
private Integer objField1;
private List<Integer> listIntegerField;
}
@Data
public static class Bean2{
private int intField;
private String strField;
private Object objField;
private Object objField1;
private List<String> listIntegerField;
}
}
 

 

总结:可用阿里的json转换工具处理类似的属性赋值。其强转赋值对能用字符串强转的类型赋值更智能,属性元素的异常赋值检查也更严格。

 

原文链接:https://www.cnblogs.com/swz1104919/p/11743771.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:读书笔记---《编写可读代码的艺术》

下一篇:一起学Spring之注解和Schema方式实现AOP