원본 객체를 복사할 때 setter메소드로 복사를 하는데 개수가 많아지면 코드도 길어지고 번거로워진다.

그래서 BeanUtils 클래스의 copyProperties 메소드를 이용하려고 한다.

 

copyProperties 메소드

 : 원본 객체를 복사할 때 사용하는 메소드

 

사용방법

BeanUtils.copyProperties(source, target);
 : source(원본), target(복사대상)

import org.springframework.beans.BeanUtils;

public class copyPropertiesTest {
    static class A{
        private  int num;
        private String id;

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }
    }

    static  class B{
        private int num;
        private String id;

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }
    }

    public static void main(String args[]){
        A a=new A();
        a.setNum(1);
        a.setId("A1");

        B b=new B();
        b.setNum(2);
        b.setId("B2");
		
        System.out.println("===========================");
        System.out.println("b.getNum() : " + b.getNum());
        System.out.println("b.getId() : " + b.getId());
        
        BeanUtils.copyProperties(a,b);
        
        System.out.println("b.getNum() : " + b.getNum());
        System.out.println("b.getId() : " + b.getId());
        System.out.println("===========================");
    }

}

 

필드가 많지 않을 경우에는 b.setId(a.getId()); 로 필드를 하나씩 설정해서 데이터를 가져올 수 있지만 필드가 많은 경우에는  BeanUtils.copyProperties(a,b); 를 사용해서 한 번에 복사할 수 있다.

 

[출력]

 

+ 특정 필드의 복사를 제외하고 싶은 경우 

 

사용방법

BeanUtils.copyProperties(source, target, ignoreProperties);

 : ignoreProperties(복사에서 제외할 필드)

package com.example.pharmacyTest;

import org.springframework.beans.BeanUtils;

public class copyPropertiesTest {
    static class A{
        private  int num;
        private String id;
        private String name;

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    static  class B{
        private int num;
        private String id;
        private String name;

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public static void main(String args[]){
        A a=new A();
        a.setNum(1);
        a.setId("A1");
        a.setName("AA1");

        B b=new B();
        b.setNum(2);
        b.setId("B2");
        b.setName("BB1");

        System.out.println("===========================");
        System.out.println("b.getNum() : " + b.getNum());
        System.out.println("b.getId() : " + b.getId());
        System.out.println("b.getName() : " + b.getName());
        BeanUtils.copyProperties(a,b,"id","name");
        System.out.println("===========================");
        System.out.println("b.getNum() : " + b.getNum());
        System.out.println("b.getId() : " + b.getId());
        System.out.println("b.getName() : " + b.getName());
        System.out.println("===========================");

    }

}

id와 name을 제외하고 필드를 복사하고 싶은 경우 ignoreProperties에 "id","name"을 넣어주면 된다.

 

[결과]

 

 

자세한 설명은 아래에 링크에서 확인하면 된다.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/BeanUtils.html

 

BeanUtils (Spring Framework 5.3.7 API)

static boolean isSimpleValueType(Class  type) Check if the given type represents a "simple" value type: a primitive or primitive wrapper, an enum, a String or other CharSequence, a Number, a Date, a Temporal, a URI, a URL, a Locale, or a Class.

docs.spring.io

+ Recent posts