How to use reflection to copy properties from Pojo to other Java Beans
Sometimes we need copy properties from a Java class to other, we can do it this manually or with our own reflection implementation, but in this case use us reflection for automate it with a utility from apache
Requirements
- commons-beanutils , you can download from here http://commons.apache.org/beanutils/
- commons-loging , you can download from here http://commons.apache.org/logging/
- Eclipse
- JDK 1.6
2. Hands on
- Create a “Java Project” in eclipse.
- Project name: CopyProperties, and click on “Finish” button.
- Unzip both
common-beanutils-xxx.zip
andcommons-logging-xxx.zip
. - Add
commons-beanutils-xxx.jar
andcommons-logging-xxx.jar
to the classpath of your project.
Create a new class “Person” in the package pojo.from
package pojo.from;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Create a class “OthePerson” in pojo.to
package with same fields
package pojo.to;
public class OthePerson {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3. Testing
Create a test class in pojo.test
package with main method, and test commons-beanutils
.
package pojo.test;
import org.apache.commons.beanutils.BeanUtils;
import pojo.from.Person;
import pojo.to.OthePerson;
/**
* Class for test copy properties
*
* @author Rene Enriquez
* @date 23/07/2012
*
*/
public class Test {
/**
* Main method
*
* @param args
*/
public static void main(String[] args) throws Exception {
Person person = new Person();
person.setAge(15);
person.setName("rene");
OtherPerson othePerson = new OtherPerson();
System.out.println("*** Before BeanUtils.copyProperties ***");
System.out.println("Person");
System.out.println(person.getAge());
System.out.println(person.getName());
System.out.println("othePerson");
System.out.println(othePerson.getAge());
System.out.println(othePerson.getName());
//copy properties from (target, source)
BeanUtils.copyProperties(othePerson, person);
System.out.println("\n*** After BeanUtils.copyProperties ***");
System.out.println("Person");
System.out.println(person.getAge());
System.out.println(person.getName());
System.out.println("othePerson");
System.out.println(othePerson.getAge());
System.out.println(othePerson.getName());
}
}
Output
*** Before BeanUtils.copyProperties ***
Person
15
rene
othePerson
0
null
*** After BeanUtils.copyProperties ***
Person
15
rene
othePerson
15
rene
Download Source Code
Download it – CopyProperties.zip (6 KB)
this only works with objects which are having public fields or private fields with both getters and setters..this does not apply to objects we creating using builder pattern as they don’t have public setters.
Link exchange is nothing else however it is just placing the other person’s webpage link on your page at proper place and other person will also do similar in favor of you.
What is the difference between shallow copy and deep copy?
http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy
What you mean with shallow copy and deep copy?…references and values????
This only works fine as long as there are no complex properties. Using Dozer (https://github.com/DozerMapper/dozer/) is a better and more scalable approach IMHO.
MapStruct is also good option for this and also support annotation
yes dozer is best way to use mapping for pojo
is use this approach in my project and suggest it
I was also thinking about Dozer when I saw this post. Great solution and I used it several times already.
Is this also known as “Prototype” pattern in GO4?
I guess this is good for “Audit Trail” purpose for Enterprise applications.
Yes, is an implementation example of prototype pattern