copyProperties method BeanUtils of Spring precautions for use

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();
        //未copy内部类的属性
        BeanUtils.copyProperties(copyTest1, copyTest2);
        System.out.println("copy内部类的属性前copyTest2 = " + copyTest2);
        CopyTest2.InnerClass innerClass2 = new CopyTest2.InnerClass();
        copyTest2.innerClass = innerClass2;
        BeanUtils.copyProperties(innerClass, innerClass2);
        System.out.println("copy内部类的属性后copyTest2 = " + copyTest2);

    }

}

to sum up

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

  2. If the same internal attributes Existence, but not inside the same class, i.e. belongs to the respective internal class, different spring properties that will not copy;

  3. Only generics at compile time work, do not rely on generic restricted operating period;

  4. Finally, the location method of copy source and destination parameters and properties of the spring apache just the opposite, so the lead pack and when the call should take note.

Only generics at compile time work, do not restrict rely on generic run-of

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");  //编译器报错
        Class clazz3 = Class.forName("java.util.ArrayList");//获取ArrayList的字节码文件
        Method m = clazz3.getMethod("add", Object.class);//获取add() 方法,Object.class 代表任意对象类型的数据
        m.invoke(list,"xiaobu");//通过反射添加字符串类型元素数据
        System.out.println(list);//运行结果:[111, 222, xiaobu]
    }
}

Published 152 original articles · won praise 18 · views 70000 +

Guess you like

Origin blog.csdn.net/tanhongwei1994/article/details/102388870