当前位置:网站首页>BeanUtils -- shallow copy -- example / principle
BeanUtils -- shallow copy -- example / principle
2022-07-02 13:48:00 【It blade out of sheath】
Original website :BeanUtils-- Shallow copy -- example / principle _IT A blog with a sharp blade -CSDN Blog
brief introduction
explain
This paper introduces BeanUtils Copy principle of .
summary
BeanUtils Is a shallow copy .
If the member to be copied is a basic data type , It copies the value of the member , Equivalent to data is independent . The basic data types here include basic type and packaging type , for example :Integer、int、String、Long、long etc. )
If the member to be copied is not a basic data type , Just copy the reference address ( Use the same reference address ( That is to say : Same object )).
example
Code
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(" Shanghai ");
user.setUserDetail(userDetail);
User user1 = new User();
BeanUtils.copyProperties(user, user1);
return "test success";
}
}
test
Breaking point :

visit :http://localhost:8080/test
result :

You can see ,user and user1 Their userDetail Members are the same object :{[email protected]}
Principle analysis
Breaking point

visit :http://localhost:8080/test
Run to this place :
BeanUtils#copyProperties(java.lang.Object, java.lang.Object, java.lang.Class<?>, java.lang.String...)
As shown in the figure below :

You can see ,targetPds It contains all members of the target object , Deal with it in turn , Look at the code in the lower part :

You can find , It uses reflection , First call the getXxx Method gets value , Then call the... Of the target object setXxx Method to set the value .
therefore , When the member is an object other than the basic type , it getXxx You will get the reference address ,setXxx Will set the reference address , So it will share the same object .
边栏推荐
- JS reverse row query data decryption
- Unity skframework framework (XII), score scoring module
- selenium 在pycharm中安装selenium
- Qt-制作一个简单的计算器-实现四则运算
- [技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
- Explanation of 34 common terms on the Internet
- Student course selection information management system based on ssm+jsp framework [source code + database]
- Why can't d link DLL
- What are eNB, EPC and PGW?
- MySQL -- convert rownum in Oracle to MySQL
猜你喜欢

代码实现MNLM

Chaos engineering platform chaosblade box new heavy release
![[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](/img/42/21f6d0fdd159faa8b63713624c95a2.png)
[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

诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...

瀏覽器驅動的下載

A better database client management tool than Navicat
![[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology](/img/a7/44609a5acf25021f1fca566c3d8c90.png)
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology

题解:《压缩技术》(原版、续集版)

2022零代码/低代码开发白皮书【伙伴云出品】附下载

Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
随机推荐
Just 1000 fans, record it
Fundamentals of machine learning (II) -- division of training set and test set
Astro learning notes
Chinese name extraction (toy code - accurate head is too small, right to play)
SSL证书的分类有哪些?如何选择合适的SSL证书?
Find love for speed in F1 delta time Grand Prix
瀏覽器驅動的下載
Node.js通过ODBC访问PostgreSQL数据库
On flow delivery between microservices
selenium,元素操作以及浏览器操作方法
你的 Sleep 服务会梦到服务网格外的 bookinfo 吗
题解《子数整数》、《欢乐地跳》、《开灯》
mysql ---- Oracle中的rownum转换成MySQL
[USACO05JAN]Watchcow S(欧拉回路)
selenium的特点
P3008 [USACO11JAN]Roads and Planes G (SPFA + SLF优化)
Why is the default of switch followed by break?
Engineers who can't read device manuals are not good cooks
P3008 [usaco11jan]roads and planes g (SPFA + SLF optimization)
Android kotlin material design technology points