当前位置:网站首页>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 .
边栏推荐
猜你喜欢
Pointer from entry to advanced (1)
Node. JS accessing PostgreSQL database through ODBC
qt中uic的使用
Unity skframework framework (XII), score scoring module
Research shows that "congenial" is more likely to become friends
Error function ERF
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
ensp简单入门
Redis database persistence
为什么switch 的default后面要跟break?
随机推荐
Origin绘制热重TG和微分热重DTG曲线
D如何检查null
MAC (MacOS Monterey 12.2 M1) personal use PHP development
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
Daily question: 1175 Prime permutation
你的 Sleep 服务会梦到服务网格外的 bookinfo 吗
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
On flow delivery between microservices
OpenAPI generator: simplify the restful API development process
JS reverse row query data decryption
科技的成就(二十七)
Can automatically update the universal weekly report template, you can use it with your hand!
I did it with two lines of code. As a result, my sister had a more ingenious way
Qt-制作一个简单的计算器-实现四则运算
运维必备——ELK日志分析系统
验证失败,请检查您的回电网址。您可以按照指导进行操作
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
Dingtalk send message
2022 Heilongjiang provincial examination on the writing skills of Application Essays
How to explain binary search to my sister? This is really difficult, fan!