当前位置:网站首页>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 .
边栏推荐
- Explanation of 34 common terms on the Internet
- Add sequence number column to query results in MySQL
- JS reverse row query data decryption
- Skillfully use SSH to get through the Internet restrictions
- [USACO05JAN]Watchcow S(欧拉回路)
- Verification failed, please check your call back website. You can follow the instructions
- The 29 year old programmer in Shanghai was sentenced to 10 months for "deleting the database and running away" on the day of his resignation!
- Redis database persistence
- Three methods of finding LCA of the nearest common ancestor
- 2022 zero code / low code development white paper [produced by partner cloud] with download
猜你喜欢

代码实现MNLM

错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”

Solution: Compression Technology (original version and sequel version)

Can automatically update the universal weekly report template, you can use it with your hand!

qt中uic的使用

I did it with two lines of code. As a result, my sister had a more ingenious way

Essential for operation and maintenance - Elk log analysis system

Bridge of undirected graph
![[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

We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
随机推荐
Subcontracting configuration of uniapp applet subpackages
P3008 [usaco11jan]roads and planes g (SPFA + SLF optimization)
Drawing Nyquist diagram with MATLAB
题解:《压缩技术》(原版、续集版)
On flow delivery between microservices
What are the classifications of SSL certificates? How to choose the appropriate SSL certificate?
诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...
Skillfully use SSH to get through the Internet restrictions
A better database client management tool than Navicat
Numpy array calculation
Why is the default of switch followed by break?
Astro learning notes
Clean up system cache and free memory under Linux
How to modify the error of easydss on demand service sharing time?
Download files and preview pictures
P1347 sorting (topology + SPFA judgment ring or topology [inner judgment ring])
2022 zero code / low code development white paper [produced by partner cloud] with download
selenium,元素操作以及浏览器操作方法
使用BLoC 构建 Flutter的页面实例
uniapp小程序 subPackages分包配置