当前位置:网站首页>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会将引用地址设置进去,所以会共用同一个对象。
边栏推荐
- Lucky numbers in the [leetcode daily question] matrix
- numpy数组计算
- Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
- leetcode621. 任务调度器
- 石子合并板子【区间DP】(普通石子合并 & 环形石子合并)
- P3008 [USACO11JAN]Roads and Planes G (SPFA + SLF优化)
- [技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
- Redis database persistence
- 2022 zero code / low code development white paper [produced by partner cloud] with download
- 为什么switch 的default后面要跟break?
猜你喜欢
Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
Bridge of undirected graph
(POJ - 1984) navigation nightare (weighted and search set)
How to explain binary search to my sister? This is really difficult, fan!
刚好1000粉丝,记录一下
Node.js通过ODBC访问PostgreSQL数据库
Unity skframework Framework (XVI), package manager Development Kit Manager
最近公共祖先LCA的三种求法
基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
随机推荐
三翼鸟两周年:羽翼渐丰,腾飞指日可待
(POJ - 1984) navigation nightare (weighted and search set)
Fundamentals of machine learning (II) -- division of training set and test set
D how to check null
你的 Sleep 服务会梦到服务网格外的 bookinfo 吗
Engineers who can't read device manuals are not good cooks
Sum of the first n terms of Fibonacci (fast power of matrix)
【youcans 的图像处理学习课】总目录
How to modify the error of easydss on demand service sharing time?
De4000h storage installation configuration
Daily practice of C language --- monkeys divide peaches
[Blue Bridge Cup] children's worship circle
Unity skframework framework (XII), score scoring module
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
Unity skframework Framework (XVI), package manager Development Kit Manager
Principle analysis of security rememberme
Gee learning notes 2
石子合并板子【区间DP】(普通石子合并 & 环形石子合并)
科技的成就(二十七)
文件的下载与图片的预览