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转换工具处理类似的属性赋值。其强转赋值对能用字符串强转的类型赋值更智能,属性元素的异常赋值检查也更严格。

 
上一篇:Vue3的 Composition API(一)


下一篇:org.springframework.beans.BeanUtils的用法