Application and precautions of copyProperties method based on Spring BeanUtils

  • 2021-09-16 07:02:48
  • OfStack

As shown below:


package com.demo;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import java.util.Arrays;
import java.util.List;
/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2019/10/8 10:04
 * @description
 */
public class BeanUtilsTest {
    @Data
    private static class CopyTest1 {
        private String outerName;
        private CopyTest1.InnerClass innerClass;
        private List clazz;
        @Data
        private static class InnerClass {
            public String innerName;
        }
    }
    @Data
    private static class CopyTest2 {
        private String outerName;
        private CopyTest2.InnerClass innerClass;
        private List clazz;
        @Data
        static class InnerClass {
            private String innerName;
        }
    }
    public static void main(String[] args) {
        CopyTest1 copyTest1 = new CopyTest1();
        copyTest1.outerName = "outer xiaobu";
        CopyTest1.InnerClass innerClass = new CopyTest1.InnerClass();
        innerClass.innerName = "inner xiaobu";
        copyTest1.innerClass = innerClass;
        copyTest1.clazz = Arrays.asList(1, 2, 3);
        System.out.println("copyTest1 = " + copyTest1);
        CopyTest2 copyTest2 = new CopyTest2();
        // Not copy Properties of inner classes 
        BeanUtils.copyProperties(copyTest1, copyTest2);
        System.out.println("copy Before the property of the inner class copyTest2 = " + copyTest2);
        CopyTest2.InnerClass innerClass2 = new CopyTest2.InnerClass();
        copyTest2.innerClass = innerClass2;
        BeanUtils.copyProperties(innerClass, innerClass2);
        System.out.println("copy After the properties of the inner class copyTest2 = " + copyTest2);
    }
}

Summarize

1. The CopyProperties method of BeanUtils of Spring needs the corresponding attributes of getter and setter methods;

2. If there is an internal class with identical attributes, but it is not the same internal class, that is, it belongs to its own internal class, then spring will think that the attributes are different and will not copy;

3. Generics only work at compile time, and can't rely on generics to limit runtime;

4. Finally, the position of method source and destination parameters of copy attribute of spring and apache is just opposite, so pay attention to 1 when guiding package and calling.

Generics only work at compile time, and cannot be relied on for runtime restrictions


package com.demo;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2019/10/8 14:54
 * @description
 */
public class Demo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(111);
        list.add(222);
        list.add("xiaobu");  // Compiler error report 
        Class clazz3 = Class.forName("java.util.ArrayList");// Get ArrayList ByteCode file of 
        Method m = clazz3.getMethod("add", Object.class);// Get add()  Method, Object.class  Represents data of any object type 
        m.invoke(list,"xiaobu");// Adding String Type Element Data through Reflection 
        System.out.println(list);// Run results: [111, 222, xiaobu]
    }
}

Error BeanUtils. copyProperties

Note: Attribute replication, different methods in jar, different usage!

Spring package (org. springframework. beans)


BeanUtils.copyProperties(A,B);

Is the value in A assigned to B

Apache package (org. apache. commons. beanutils) (commonly used)


BeanUtils.copyProperties(A,B);

Is the value in B assigned to A


Related articles: