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);
}
}
OutputEmployee{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);
}
}
OutputPerson{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);
}
}
OutputSquare{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);
}
}
OutputSquare{length=5, color='null'}
Example ProjectDependencies and Technologies Used: - spring-context 6.1.2 (Spring Context)
Version Compatibility: 3.2.6.RELEASE - 6.1.2 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 17
- Maven 3.8.1
|