当前位置:网站首页>BeanUtils--浅拷贝--实例/原理
BeanUtils--浅拷贝--实例/原理
2022-07-02 10:18:00 【IT利刃出鞘】
原文网址:BeanUtils--浅拷贝--实例/原理_IT利刃出鞘的博客-CSDN博客
简介
说明
本文用实例介绍BeanUtils的拷贝原理。
概述
BeanUtils是浅拷贝。
如果要拷贝的成员是基本数据类型,它会拷贝成员的值,相当于数据是独立的。这里的基本数据类型包括基础类型和包装类型,例如:Integer、int、String、Long、long等)
如果要拷贝的成员不是基本数据类型,就拷贝引用地址(使用同一个引用地址(也就是:同一个对象))。
实例
代码
Entity
User
package com.knife.entity;
import lombok.Data;
@Data
public class User {
private Long id;
private String userName;
private UserDetail userDetail;
}
UserDetail
package com.knife.entity;
import lombok.Data;
@Data
public class UserDetail {
private Long id;
private String address;
}
Controller
package com.knife.controller;
import com.knife.entity.User;
import com.knife.entity.UserDetail;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/test")
public String test() {
User user = new User();
user.setId(1L);
user.setUserName("Tony");
UserDetail userDetail = new UserDetail();
userDetail.setId(2L);
userDetail.setAddress("上海");
user.setUserDetail(userDetail);
User user1 = new User();
BeanUtils.copyProperties(user, user1);
return "test success";
}
}
测试
打断点:
结果:
可以看到,user和user1它们的userDetail成员是同一个对象:{[email protected]}
原理分析
打断点
运行到这个地方:
BeanUtils#copyProperties(java.lang.Object, java.lang.Object, java.lang.Class<?>, java.lang.String...)
如下图所示:
可以看到,targetPds里边包含了目标对象的所有成员,依次对它处理,看它下半部分的代码:
可以发现,它是使用反射,先调用源对象的getXxx方法获取值,再调用目标对象的setXxx方法将值设置进去。
所以,当成员是基本类型之外的对象时,它getXxx会获取到引用地址,setXxx会将引用地址设置进去,所以会共用同一个对象。
边栏推荐
- Answer: can the audio be set to on by default during easydss video on demand?
- Pointer from entry to advanced (1)
- [OpenGL] notes 29. Advanced lighting (specular highlights)
- Engineers who can't read device manuals are not good cooks
- MAC (MacOS Monterey 12.2 M1) personal use PHP development
- 解答:EasyDSS视频点播时音频是否可以设置为默认开启?
- 【模板】最长公共子序列 (【DP or 贪心】板子)
- 每日一题:1175.质数排列
- Unity skframework framework (XIX), POI points of interest / information points
- rxjs Observable 自定义 Operator 的开发技巧
猜你喜欢
Web Foundation
屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
EasyDSS点播服务分享时间出错如何修改?
2022零代码/低代码开发白皮书【伙伴云出品】附下载
Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
OpenFOAM:lduMatrix&lduAddressing
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
运维必备——ELK日志分析系统
Unity skframework framework (XII), score scoring module
使用BLoC 构建 Flutter的页面实例
随机推荐
Bridge of undirected graph
Engineers who can't read device manuals are not good cooks
Pointer from entry to advanced (1)
SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
使用BLoC 构建 Flutter的页面实例
Integral link, inertia link and proportion link in Simulink
你的 Sleep 服务会梦到服务网格外的 bookinfo 吗
屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
Unity skframework Framework (XVI), package manager Development Kit Manager
【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
2022 Heilongjiang provincial examination on the writing skills of Application Essays
Astro learning notes
EasyDSS点播服务分享时间出错如何修改?
大家信夫一站式信用平台让信用场景“用起来
mysql ---- Oracle中的rownum转换成MySQL
Achievements in science and Technology (27)
Unity skframework framework (XVI), package manager development kit Manager