web123456

Use (deep copy, shallow copy)

Article Directory

    • Scene
      • Is BeanUtils a deep copy or a shallow copy?
    • What is the right situation to use BeanUtils
      • If there are child objects, you must not use BeanUtils
    • Code example
      • dest ,src or src,dest

What we are talking about here is spring.

Scene

It is often encountered during development that the attributes of the parent class are copied to the subclass. There are usually 2 methods:
1. Set one by one
2. Use
Obviously, BeanUtils is more convenient and much more beautiful.
So can you use BeanUtils in any situation? Of course not. You must first understand him.

Is BeanUtils a deep copy or a shallow copy?

It's a shallow copy.
Shallow copy: Just call the set method of the child object, and does not copy all attributes. (That is, a memory address referenced)
Deep copy: Copy the properties of the child object as well.

What is the right situation to use BeanUtils

If they are all single attributes, then the issue of deep copy does not involve, and it is suitable to use BeanUtils.

If there are child objects, you must not use BeanUtils

Not absolutely, this should be considered differently:
1. The sub-object needs to be changed.
2. The sub-object does not change much.
Although there are child objects, the child objects are not changed much, so it is no problem to use BeanUtils.

Code example

The following is explained with code.
Cuishan has a son Wuji, who inherits his face and height.
But life should be its own.
Later, Cuishan committed suicide and Wuji also became dead. This is the shallow copy, Wuji used life referenced by Cuishan's life object.

Father class:

@Data
public class Father {
    private String face; // Look
    private String height; // height
    private Life life; // life
}

Life Class:

@Data
public class Life {
    private String status;
}

Son class and main method:

@Data
public class Son extends Father{
    private Life life;

    public static void main(String[] args) {
        Father cuishan = new Father();
        cuishan.setFace("handsome");
        cuishan.setHeight("180");
        Life cuishanLife = new Life();
        cuishanLife.setStatus("alive");
        cuishan.setLife(cuishanLife);
        Son wuji=new Son();
        BeanUtils.copyProperties(cuishan,wuji);

//        Life wujiLife = ();
//        ("alive");
//        (wujiLife);
// ("dead"); // The Cuishan came from behind and died

        System.out.println(JSON.toJSONString(cuishan));
        System.out.println(JSON.toJSONString(wuji));
    }
}

The code commented above can be replaced as follows:
case1 and case2 are still affected by shallow copies, case3 is not affected.
case1: Cuishan committed suicide, Wuji died

//        Life wujiLife = ();
//        ("alive");
//        (wujiLife);
// ("dead"); // The Cuishan came from behind and died

case2: Cuishan committed suicide, Wuji set up or Cuishan also survived

// ("dead"); // The Cuishan came from behind and died
//        Life wujiLife = ();
//        ("alive");
//        (wujiLife);

case3: Cuishan and Wuji have no influence on each other

		cuishanLife.setStatus("dead"); // Cuishan committed suicide. This trip can be placed up and down.
        // Wuji uses a new object and is not affected by Cuishan
        Life wujiLife = new Life();
        wujiLife.setStatus("alive");
        wuji.setLife(wujiLife);

dest ,src or src,dest

The author has climbed through the pit here.
Because I remember that there is the beanutils tool, I directly import it.
I found copyProperty and copyProperties. After looking at it, I found that it was dest and src, so I used it decisively, but found that the parameters were gone.

In fact, there are 2 common BeanUtils:
spring has BeanUtils
Commons in apache also have BeanUtils.

The differences are as follows:

spring's BeanUtils Commons' BeanUtils
method copyProperty and copyProperties copyProperties
parameter src ,dest dest,src

Both of these two are fine, but pay attention to the difference. Because the src and dest of their two are exactly the opposite, please pay special attention.