当前位置:网站首页>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 .
边栏推荐
- Subcontracting configuration of uniapp applet subpackages
- Let juicefs help you with "remote backup"
- 693. Travel sequencing (map + topology)
- 2022零代码/低代码开发白皮书【伙伴云出品】附下载
- Use bloc to build a page instance of shutter
- Node. JS accessing PostgreSQL database through ODBC
- 刚好1000粉丝,记录一下
- Pointer from entry to advanced (1)
- 代码实现MNLM
- D language, possible 'string plug-ins'
猜你喜欢
Drawing Nyquist diagram with MATLAB
2022零代码/低代码开发白皮书【伙伴云出品】附下载
Selenium installing selenium in pycharm
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
How to modify the error of easydss on demand service sharing time?
Gee learning notes 2
题解:《你的飞碟在这儿》、《哥德巴赫猜想》
What are eNB, EPC and PGW?
[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
(POJ - 1984) navigation nightare (weighted and search set)
2022 zero code / low code development white paper [produced by partner cloud] with download
Solution: Compression Technology (original version and sequel version)
Node.js通过ODBC访问PostgreSQL数据库
题解:《压缩技术》(原版、续集版)
免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
Integral link, inertia link and proportion link in Simulink
Detailed collection of common MySQL commands
运维必备——ELK日志分析系统
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
Security RememberMe原理分析
三谈exception——错误处理
Why can't d link DLL
Characteristics of selenium
Don't spend money, spend an hour to build your own blog website
OpenAPI generator: simplify the restful API development process
Verification failed, please check your call back website. You can follow the instructions
Explanation of 34 common terms on the Internet