Spring BeanUtils 기반 copy Properties 방법 사용 및 주의사항

다음 과 같다.

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);
    }
}
총결산
1.Spring 의 BeanUtils 의 Copy Properties 방법 에 대응 하 는 속성 은 getter 와 setter 방법 이 있 습 니 다.
2.속성 이 똑 같은 내부 류 가 존재 하지만 같은 내부 류 가 아 닌 지,즉 각자 의 내부 류 에 속 하면 spring 은 속성 이 다 르 고 copy 가 없다 고 생각 합 니 다.
3.범 형 은 컴 파일 기간 에 만 작용 하고 범 형 에 의 해 운행 기한 의 제한 을 할 수 없다.
4.마지막 으로 spring 과 apache 의 copy 속성 방법 원 과 목적 매개 변수 의 위치 가 정반 대 이기 때문에 패키지 와 호출 할 때 주의해 야 합 니 다.
범 형 은 컴 파일 기간 에 만 작용 하 며,범 형 에 의존 하여 운행 기간 의 제한 을 할 수 없다.

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]
    }
}
BeanUtils.copy 속성 오류
메모:속성 복사,jar 의 방법 에 따라 용법 이 다 릅 니 다!
Spring 패키지(org.spring framework.beans)중

BeanUtils.copyProperties(A,B);
A 의 값 입 니 다.B 에 게 할당 합 니 다.
아파 치 패키지(org.apache.comons.beanutils)중(자주 사용)

BeanUtils.copyProperties(A,B);
B 의 값 입 니 다.A 에 게 할당 합 니 다.
이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.

좋은 웹페이지 즐겨찾기