BeanUtils.copyProperties() usage example - Java object reference and assignment analysis

BeanUtils.copyProperties() usage example - Java object reference and assignment analysis

Preface

During the development of a recent project, I encountered the situation of copying an object and modifying one of its attribute values. I did not realize that there was any problem at the time. It was not until later during testing that I found that not only the attribute value of the new object had been changed, but also the attribute value of the original object. After being modified, I realized that I had made a very basic mistake, so I recorded the analysis of object references and assignment issues and the use of the BeanUtils.copyProperties() method.

object reference

Principle analysis

Objects are variables declared by a class and are responsible for storing references to ensure that the object can operate the variables assigned to the object and call methods in the class.

When we directly new two objects, the references of the two objects are different, as shown below.
Insert image description here
If we perform the following assignment operation, the two objects will have the same reference, and their references point to the same entity.

object2 = object1;

At this time, their pointing becomes
Insert image description here
Here, everyone should know the problem I mentioned in the preface. I assigned the reference of the original object to the new object. At this time, the references of the two objects are the same, that is, they point to For the same entity, when I modify the variable (attribute) value through a new object, the variable value of this entity is modified. Because the references of the original object and the new object point to this entity, it is different from my needs.

code demo

Here first create a new User class

package com.example.studytest.entity;

import lombok.Data;

/**
 * @Description:  User
 * @Author:       Olrookie
 * @Date:         2023/6/11
 */
@Data
public class User {
    
    

    /**
     * 姓名
     */
    private String name;

    /**
     * 年龄
     */
    private int age;

    /**
     * 性别
     */
    private String sex;
}

Let’s reproduce my mistake first

public void test() {
    
    
        System.out.println("test开始执行!!");

        // 新建一个原对象sourceUser
        User sourceUser = new User();
        sourceUser.setName("小陈");
        sourceUser.setAge(25);
        sourceUser.setSex("女");

        System.out.println("原始sourceUser:" + sourceUser);

        // 新建一个对象tempUser,将原对象sourceUser的引用赋值给tempUser
        User tempUser = new User();
        tempUser = sourceUser;
        System.out.println("赋值后的tempUser:" + tempUser);
        // 通过新对象tempUser修改变量age的值
        tempUser.setAge(18);
        
        System.out.println("tempUser修改Age属性后的tempUser:" + tempUser);
        System.out.println("tempUser修改Age属性后的sourceUser:" + sourceUser);
    }

The output of the code is:

test开始执行!!
原始sourceUser:User(name=小陈, age=25, sex=)
赋值后的tempUser:User(name=小陈, age=25, sex=)
tempUser修改Age属性后的tempUser:User(name=小陈, age=18, sex=)
tempUser修改Age属性后的sourceUser:User(name=小陈, age=18, sex=)

Through the output results, we can see that the age of the entity pointed to by the reference of the object sourceUser has also become 18.

BeanUtils.copyProperties() copy object

Here we directly demonstrate BeanUtils.copyProperties() copying objects through code. For convenience of comparison, we create a new object under the above code.

public void test() {
    
    
        System.out.println("test开始执行!!");

        // 新建一个原对象sourceUser
        User sourceUser = new User();
        sourceUser.setName("小陈");
        sourceUser.setAge(25);
        sourceUser.setSex("女");

        System.out.println("原始sourceUser:" + sourceUser);

        // 新建一个对象tempUser,将原对象sourceUser的引用赋值给tempUser
        User tempUser = new User();
        tempUser = sourceUser;
        System.out.println("赋值后的tempUser:" + tempUser);
        tempUser.setAge(18);
        System.out.println("tempUser修改Age属性后的tempUser:" + tempUser);
        System.out.println("tempUser修改Age属性后的sourceUser:" + sourceUser);

        // 新建一个对象targetUser,通过BeanUtils.copyProperties()方法进行对象拷贝
        User targetUser = new User();
        BeanUtils.copyProperties(sourceUser, targetUser);
        System.out.println("复制后的targetUser:" + targetUser);
        // 通过对象targetUser修改变量age
        targetUser.setAge(25);
        System.out.println("修改完Age属性后的targetUser:" + targetUser);
        System.out.println("targetUser修改完Age属性后的sourceUser:" + sourceUser);
    }

The output of the program is:

test开始执行!!
原始sourceUser:User(name=小陈, age=25, sex=)
赋值后的tempUser:User(name=小陈, age=25, sex=)
tempUser修改Age属性后的tempUser:User(name=小陈, age=18, sex=)
tempUser修改Age属性后的sourceUser:User(name=小陈, age=18, sex=)
复制后的targetUser:User(name=小陈, age=18, sex=)
修改完Age属性后的targetUser:User(name=小陈, age=25, sex=)
targetUser修改完Age属性后的sourceUser:User(name=小陈, age=18, sex=)

Combining the code and output results, we can know: After executing **BeanUtils.copyProperties(sourceUser, targetUser);**, the output results of targetUser and sourceUser are the same; and then when we modify the variable age to 25 through targetUser, targetUser and The results output by sourceUser are no longer the same. At this time, their references are different and point to different entities.

Conclusion

The basics are still too important. When you learn by yourself, you just want to go over it and quickly realize the needs, thus ignoring the important foundations and principles. I still have to fill in the holes slowly in the future. The above record of this problem is also a warning to myself. If there are any problems, you are welcome to point them out.

Finally, I wish everyone becomes stronger!

Guess you like

Origin blog.csdn.net/weixin_55549435/article/details/131151057