引言
在实际的项目开发过程中,我们时常会遇到 VO、BO、PO、DTO等对象之间的赋值,在对象属性较多的时候则会使用 get 和 set 方法进行赋值,但是这种方法工作量有时候实在太大了,从而很多人会选中使用 Spring 框架提供的拷贝工具 BeanUtils 的 copyProperties 方法来完成属性之间的拷贝。这样可以最大程度上降低我的手动编写对象赋值的操作,减少工作量。
既然 BeanUtils 的 copyProperties 这么方便,为什么说不推荐使用呢,接下来说一说 BeanUtils.copyProperties() 方法拷贝数据的一些坑 。
一、对象属性不一致而拷贝失败
public class BeanUtilsTest {
public static void main(String[] args) {
SourcePoJo sourcePoJo = new SourcePoJo("jingdong", (long) 35711);
TargetPoJo targetPoJo = new TargetPoJo();
BeanUtils.copyProperties(sourcePoJo,targetPoJo);
System.out.println(targetPoJo);
}
}
@Data
@AllArgsConstructor
class SourcePoJo{
private String username;
private Long id;
}
@Data
class TargetPoJo{
private String username;
private String id;
}
二、null 值覆盖导致拷贝异常
在开发工程中,经常会遇到将一个对象的部分字段复制到了另一个对象中,如果拷贝的数据中的某个字段有 null 值存在, 但是对应需要拷贝过的对应字段不为 null ,若直接使用 BeanUtils.copyProperties() 进行数据拷贝,则被拷贝的数据上的 null 值 则会覆盖到目标字段上。
public class BeanUtilsTest {
public static void main(String[] args) {
SourcePoJo sourcePoJo = new SourcePoJo();
sourcePoJo.setId("35711");
TargetPoJo targetPoJo = new TargetPoJo();
targetPoJo.setUsername("Joy");
BeanUtils.copyProperties(sourcePoJo,targetPoJo);
System.out.println(targetPoJo);
}
}
@Data
class SourcePoJo{
private String username;
private String id;
}
@Data
class TargetPoJo{
private String username;
private String id;
}
三、查询不到字段引用,修改内容难以追溯
在项目开发或排查问题过程中,在链路中查询某个字段值的来源,如果使用的 BeanUtils.copyProperties() 方法拷贝的数据将会很难快速定位到赋值的地方,
四、不使用 BeanUtils.copyProperties() 用什么?
推荐使用spring中的@mapper(componentModel = “spring”)
定义一个interfece,不需要自己去实现,不用担心它会编译不通过,@mapper(componentModel = “spring”)这个玩意会帮你过去。项目启动后,它会自己生成他的实现类,如下
原文始发于微信公众号(Java技术前沿):为什么在项目中不推荐使用Beanutils进行属性拷贝了?
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/299631.html