BeanUtils.copyProperties 属性复制List的坑

BeanUtils.copyProperties 大家都知道是用来作为属性复制的工具,最近在项目遇到了一个坑

class A{
    private String name;
    private List cats;
    private String age;
}
class Cat{
    private String color;
    private String price;
    private String count;

}


class A1{
    private String name;
    private List cats;
}
class Cat1{
    private String color;
    private String price;

}

//开始复制
A a=new A();
a.setName("cd");
a.setAge("11");
Cat cat=new Cat();
cat.setColor("red");
cat.setPrice("200");
cat.setCount("20");
a.setCat(cat);

A1 a1=new A1();
BeanUtils.copyProperties(a1,a);
//我们想得到的a1为
a1:{"name":"cd","cat":{"price":"200","color":"red"}}
//实际的结果为
a1:{"name":"cd","cat":{"price":"200","color":"red","count":"20"}}
//所以大家进行属性复制的时候注意List,当有List的时候我们要取出来一个一个复制

 

你可能感兴趣的:(Java小知识,属性复制,Java)