Close

Spring - Copying properties using BeanUtils

[Last Updated: Jun 18, 2021]

Spring's BeanUtils provides following methods to copy property values of the given source bean into the target bean.

public static void copyProperties(Object source, Object target) 
                                                    throws BeansException
public static void copyProperties(Object source, Object target, Class<?> editable)
                                                                         throws BeansException

Calling above method will copy only properties which are defined in the given "editable" class (or interface).

public static void copyProperties(Object source, Object target, String... ignoreProperties) 
                                                                               throws BeansException

Calling above method will ignore the given "ignoreProperties" and copy the rest.

Examples

Beans

class Person{
  private String name;
  private Integer age;

  public String getName() {
      return name;
  }

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

  public Integer getAge() {
      return age;
  }

  public void setAge(Integer age) {
      this.age = age;
  }

  @Override
  public String toString() {
      return "Person{" +
              "name='" + name + '\'' +
              ", age=" + age +
              '}';
  }
}
public class Employee {
  private String name;
  private Integer age;
  private String dept;

  public String getName() {
      return name;
  }

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

  public Integer getAge() {
      return age;
  }

  public void setAge(Integer age) {
      this.age = age;
  }

  public String getDept() {
      return dept;
  }

  public void setDept(String dept) {
      this.dept = dept;
  }

  @Override
  public String toString() {
      return "Employee{" +
              "name='" + name + '\'' +
              ", age=" + age +
              ", dept='" + dept + '\'' +
              '}';
  }
}

Using BeanUtils#copyProperties(Object source, Object target)

In this example we are copying Person to Employee. Since Person does not have dept property it will be empty in the resultant Employee object.

package com.logicbig.example;

import org.springframework.beans.BeanUtils;

public class CopyPropsExample {
    public static void main (String[] args) {
       Person person = new Person();
       person.setName("Mike");
       person.setAge(34);

       Employee employee = new Employee();
       BeanUtils.copyProperties(person, employee);
        System.out.println(employee);
    }
}

Output

Employee{name='Mike', age=34, dept='null'}

In this example we are copying Employee to Person. Since Employee has an extra field of dept it will be ignored.

package com.logicbig.example;

import org.springframework.beans.BeanUtils;

public class CopyPropsExample2 {
    public static void main(String[] args) {
        Employee employee = new Employee();
        employee.setName("Mike");
        employee.setAge(34);
        employee.setDept("IT");

        Person person = new Person();
        BeanUtils.copyProperties(employee, person);
        System.out.println(person);
    }
}

Output

Person{name='Mike', age=34}

Using copyProperties(Object source, Object target, Class<?> editable)

This method copies the property values of the given source bean into the given target bean, only setting properties defined in the given "editable" class (or interface).

The source and target classes do not have to match or even be derived from each other, as long as the properties match.

The target object must be an instance of editable class/interface.

package com.logicbig.example;

public interface Shape {
  String getColor();
  void setColor(String color);
}
package com.logicbig.example;

public class Rectangle implements Shape {
  private Integer width;
  private Integer length;
  private String color;

  public Integer getWidth() {
      return width;
  }

  public void setWidth(Integer width) {
      this.width = width;
  }

  public Integer getLength() {
      return length;
  }

  public void setLength(Integer length) {
      this.length = length;
  }

  @Override
  public String getColor() {
      return color;
  }

  public void setColor(String color) {
      this.color = color;
  }

  @Override
  public String toString() {
      return "Rectangle{" +
              "width=" + width +
              ", length=" + length +
              ", color='" + color + '\'' +
              '}';
  }
}
package com.logicbig.example;

public class Square implements Shape{
  private Integer length;
  private String color;

  public Integer getLength() {
      return length;
  }

  public void setLength(Integer length) {
      this.length = length;
  }

  @Override
  public String getColor() {
      return color;
  }

  public void setColor(String color) {
      this.color = color;
  }

  @Override
  public String toString() {
      return "Square{" +
              "length=" + length +
              ", color='" + color + '\'' +
              '}';
  }
}
package com.logicbig.example;

import org.springframework.beans.BeanUtils;

public class CopyPropsEditableExample {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle();
        rectangle.setColor("blue");
        rectangle.setLength(5);
        rectangle.setWidth(4);

        Square square = new Square();
        BeanUtils.copyProperties(rectangle, square, Shape.class);
        System.out.println(square);
    }
}

Output

Square{length=null, color='blue'}

Using copyProperties(Object source, Object target, String... ignoreProperties)

package com.logicbig.example;

import org.springframework.beans.BeanUtils;

public class CopyPropsIgnoreExample {
  public static void main(String[] args) {
      Rectangle rectangle = new Rectangle();
      rectangle.setColor("blue");
      rectangle.setLength(5);
      rectangle.setWidth(4);

      Square square = new Square();
      BeanUtils.copyProperties(rectangle, square, "color");
      System.out.println(square);
  }
}

Output

Square{length=5, color='null'}

Example Project

Dependencies and Technologies Used:

  • spring-context 5.3.8: Spring Context. Version Compatibility: 3.2.6.RELEASE - 5.3.8 Version List
    ×

    Version compatibilities of spring-context with this example:

    • 3.2.6.RELEASE
    • 3.2.7.RELEASE
    • 3.2.8.RELEASE
    • 3.2.9.RELEASE
    • 3.2.10.RELEASE
    • 3.2.11.RELEASE
    • 3.2.12.RELEASE
    • 3.2.13.RELEASE
    • 3.2.14.RELEASE
    • 3.2.15.RELEASE
    • 3.2.16.RELEASE
    • 3.2.17.RELEASE
    • 3.2.18.RELEASE
    • 4.0.0.RELEASE
    • 4.0.1.RELEASE
    • 4.0.2.RELEASE
    • 4.0.3.RELEASE
    • 4.0.4.RELEASE
    • 4.0.5.RELEASE
    • 4.0.6.RELEASE
    • 4.0.7.RELEASE
    • 4.0.8.RELEASE
    • 4.0.9.RELEASE
    • 4.1.0.RELEASE
    • 4.1.1.RELEASE
    • 4.1.2.RELEASE
    • 4.1.3.RELEASE
    • 4.1.4.RELEASE
    • 4.1.5.RELEASE
    • 4.1.6.RELEASE
    • 4.1.7.RELEASE
    • 4.1.8.RELEASE
    • 4.1.9.RELEASE
    • 4.2.0.RELEASE
    • 4.2.1.RELEASE
    • 4.2.2.RELEASE
    • 4.2.3.RELEASE
    • 4.2.4.RELEASE
    • 4.2.5.RELEASE
    • 4.2.6.RELEASE
    • 4.2.7.RELEASE
    • 4.2.8.RELEASE
    • 4.2.9.RELEASE
    • 4.3.0.RELEASE
    • 4.3.1.RELEASE
    • 4.3.2.RELEASE
    • 4.3.3.RELEASE
    • 4.3.4.RELEASE
    • 4.3.5.RELEASE
    • 4.3.6.RELEASE
    • 4.3.7.RELEASE
    • 4.3.8.RELEASE
    • 4.3.9.RELEASE
    • 4.3.10.RELEASE
    • 4.3.11.RELEASE
    • 4.3.12.RELEASE
    • 4.3.13.RELEASE
    • 4.3.14.RELEASE
    • 4.3.15.RELEASE
    • 4.3.16.RELEASE
    • 4.3.17.RELEASE
    • 4.3.18.RELEASE
    • 4.3.19.RELEASE
    • 4.3.20.RELEASE
    • 4.3.21.RELEASE
    • 4.3.22.RELEASE
    • 4.3.23.RELEASE
    • 4.3.24.RELEASE
    • 4.3.25.RELEASE
    • 4.3.26.RELEASE
    • 4.3.27.RELEASE
    • 4.3.28.RELEASE
    • 4.3.29.RELEASE
    • 4.3.30.RELEASE
    • 5.0.0.RELEASE
    • 5.0.1.RELEASE
    • 5.0.2.RELEASE
    • 5.0.3.RELEASE
    • 5.0.4.RELEASE
    • 5.0.5.RELEASE
    • 5.0.6.RELEASE
    • 5.0.7.RELEASE
    • 5.0.8.RELEASE
    • 5.0.9.RELEASE
    • 5.0.10.RELEASE
    • 5.0.11.RELEASE
    • 5.0.12.RELEASE
    • 5.0.13.RELEASE
    • 5.0.14.RELEASE
    • 5.0.15.RELEASE
    • 5.0.16.RELEASE
    • 5.0.17.RELEASE
    • 5.0.18.RELEASE
    • 5.0.19.RELEASE
    • 5.0.20.RELEASE
    • 5.1.0.RELEASE
    • 5.1.1.RELEASE
    • 5.1.2.RELEASE
    • 5.1.3.RELEASE
    • 5.1.4.RELEASE
    • 5.1.5.RELEASE
    • 5.1.6.RELEASE
    • 5.1.7.RELEASE
    • 5.1.8.RELEASE
    • 5.1.9.RELEASE
    • 5.1.10.RELEASE
    • 5.1.11.RELEASE
    • 5.1.12.RELEASE
    • 5.1.13.RELEASE
    • 5.1.14.RELEASE
    • 5.1.15.RELEASE
    • 5.1.16.RELEASE
    • 5.1.17.RELEASE
    • 5.1.18.RELEASE
    • 5.1.19.RELEASE
    • 5.1.20.RELEASE
    • 5.2.0.RELEASE
    • 5.2.1.RELEASE
    • 5.2.2.RELEASE
    • 5.2.3.RELEASE
    • 5.2.4.RELEASE
    • 5.2.5.RELEASE
    • 5.2.6.RELEASE
    • 5.2.7.RELEASE
    • 5.2.8.RELEASE
    • 5.2.9.RELEASE
    • 5.2.10.RELEASE
    • 5.2.11.RELEASE
    • 5.2.12.RELEASE
    • 5.2.13.RELEASE
    • 5.2.14.RELEASE
    • 5.2.15.RELEASE
    • 5.3.0
    • 5.3.1
    • 5.3.2
    • 5.3.3
    • 5.3.4
    • 5.3.5
    • 5.3.6
    • 5.3.7
    • 5.3.8

    Versions in green have been tested.

  • JDK 8
  • Maven 3.8.1

Spring - Copying properties using BeanUtils Select All Download
  • spring-bean-copy-properties
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • CopyPropsExample.java

    See Also