实体类如下:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GoodDto {
private Integer id;
private String name;
private String price;
private Description description;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GoodVo {
private Integer id;
private String name;
private Description description;
}
测试类如下:
public class BeanUtilsTest {
public static void main(String[] args) {
Description description = new Description("详情");
GoodDto goodDto = new GoodDto();
GoodVo goodVo = new GoodVo(999,"2222",description);
BeanUtils.copyProperties(goodVo,goodDto);
System.out.println(goodVo);
System.out.println("------------------------");
System.out.println(goodDto);
System.out.println(">>>>>>>>>>>>>>>>>>>>");
description.setDetail("详情信息");
goodVo.setDescription(description);
goodVo.setId(666);
System.out.println(goodVo);
System.out.println("------------------------");
System.out.println(goodDto);
}
}
输出结果如下:
结论分析:
Spring Beanuitls复制属性时,会自动忽略掉不匹配的属性,当存在引用对象复制时,改变其中一个对象时,另外一个对象信息也会随着改变,可见Spring Beanuils采用了浅拷贝方式。