JAVA基础:Spring的BeanUtils.copyProperties的使用(深拷贝,浅拷贝)

BeanUtils.copyProperties的使用(深拷贝,浅拷贝) - 小知的文章 - 知乎
https://zhuanlan.zhihu.com/p/398232699

这里说的是spring的BeanUtils.copyProperties。想看相对详细具体的JAVA知识点-BeanUtils.copyProperties() 用法,可以点击跳转我另一篇总结的文章,里面有代码举例。


首先

使用场景:开发中经常遇到,把父类的属性拷贝到子类中。通常有2种方法:

1、属性值一个一个set

	AEntity.setYyy(BEntity.getYyy());
	AEntity.setFff(BEntity.getFff());
	AEntity.setTtt(BEntity.getTtt());

2、org.springframework.beans.BeanUtils:  copyProperties(sourceDemo, targetDemo)

BeanUtils.copyProperties(AEntity,BEntity);

很显然用BeanUtils.copyProperties更加方便,也美观很多。

那么任何情况都能使用BeanUtils么,当然不是。要先了解他。


1、BeanUtils是深拷贝,还是浅拷贝?

是浅拷贝

浅拷贝:只是调用子对象的set方法,并没有将所有属性拷贝。(也就是说,引用的一个内存地址) 。
深拷贝:将子对象的属性也拷贝过去。

2、什么情况适合用BeanUtils?

如果都是单一的属性,那么不涉及到深拷贝的问题,适合用BeanUtils。

3、有子对象就一定不能用BeanUtils么?

并不绝对,这个要区分考虑:

1、子对象还要改动。
2、子对象不怎么改动。

虽然有子对象,但是子对象并不怎么改动,那么用BeanUtils也是没问题的。

代码例子

下面用代码说明下。

翠山有个儿子无忌,儿子继承了他的face和height。 但是life应该是自己的。
后来翠山自刎而死,无忌也变成dead状态了。这就是浅拷贝,无忌用的life引用的翠山的life对象。

Father类:

@Data
public class Father {
    private String face; // 长相
    private String height; // 身高
    private Life life; // 生命
}

Life 类:

@Data
public class Life {
    private String status;
}

Son类和main方法:


import com.alibaba.fastjson2.JSON;
import lombok.Data;
import org.springframework.beans.BeanUtils;
@Data
public class Son extends Father{
	private Life life;

	public static void main(String[] args) {
		Father cuishan = new Father();
		cuishan.setFace("handsome");
		cuishan.setHeight("180");
		Life cuishanLife = new Life();
		cuishanLife.setStatus("alive");
		cuishan.setLife(cuishanLife);
		Son wuji=new Son();

		BeanUtils.copyProperties(cuishan,wuji);
//		Life wujiLife = wuji.getLife();
//        wujiLife.setStatus("alive");
//        wuji.setLife(wujiLife);
//        cuishanLife.setStatus("dead"); // 翠山后来自刎了

		System.out.println(JSON.toJSONString(cuishan));
		System.out.println(JSON.toJSONString(wuji));
	}
}

上面注释出的代码可以如下替换:

case1和case2还是受浅拷贝的影响,case3不受。

case1:翠山自刎,无忌也挂了

//        Life wujiLife = wuji.getLife();
//        wujiLife.setStatus("alive");
//        wuji.setLife(wujiLife);
//        cuishanLife.setStatus("dead"); // 翠山后来自刎了
{"face":"handsome","height":"180","life":{"status":"dead"}}
{"face":"handsome","height":"180","life":{"status":"dead"}}

case2:翠山自刎,无忌设置成活,翠山也跟着活了

//        cuishanLife.setStatus("dead"); // 翠山先自刎了
//        Life wujiLife = wuji.getLife();
//        wujiLife.setStatus("alive");
//        wuji.setLife(wujiLife);
{"face":"handsome","height":"180","life":{"status":"alive"}}
{"face":"handsome","height":"180","life":{"status":"alive"}}

case3:翠山和无忌互不影响

cuishanLife.setStatus("dead"); // 翠山自刎了  该行放在上下均可
// 无忌用个新对象 不受翠山影响了
Life wujiLife = new Life();
wujiLife.setStatus("alive");
wuji.setLife(wujiLife);
{"face":"handsome","height":"180","life":{"status":"dead"}}
{"face":"handsome","height":"180","life":{"status":"alive"}}

dest ,src 还是 src,dest,笔者在这个爬过坑。

因为记得有beanutils这个工具,直接import了。

发现有copyProperty和copyProperties。看了下发现是 dest,src ,于是果断使用,结果发现参数没了。

其实常见的BeanUtils有2个:spring有BeanUtils、apache的commons也有BeanUtils。
区别如下:
在这里插入图片描述

这2个用哪个都行,但是要注意区别。因为他们2个的src和dest是正好相反的,要特别留意。

作者:enthan809882
来源:BeanUtils.copyProperties的使用(深拷贝,浅拷贝)

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值