Main Tutorials

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

  1. commons-beanutils , you can download from here http://commons.apache.org/beanutils/
  2. commons-loging , you can download from here http://commons.apache.org/logging/
  3. Eclipse
  4. JDK 1.6

2. Hands on

  1. Create a “Java Project” in eclipse.
  2. Project name: CopyProperties, and click on “Finish” button.
  3. Unzip both common-beanutils-xxx.zip and commons-logging-xxx.zip.
  4. Add commons-beanutils-xxx.jar and commons-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)

References

  1. commons-beanutils official website
  2. commons-logging official website

About Author

author image
JAVA developer

Comments

Subscribe
Notify of
11 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
ZJ
8 years ago

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.

Gander Mountain discount coupon Code
10 years ago

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.

Arun Sobti
11 years ago

What is the difference between shallow copy and deep copy?

Rene Enriquez
11 years ago
Reply to  Arun Sobti

What you mean with shallow copy and deep copy?…references and values????

Frisian
11 years ago

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.

Divyesh Kanzariya
6 years ago
Reply to  Frisian

MapStruct is also good option for this and also support annotation

ali akbar azizkhani
11 years ago
Reply to  Frisian

yes dozer is best way to use mapping for pojo
is use this approach in my project and suggest it

g00glen00b
11 years ago
Reply to  Frisian

I was also thinking about Dozer when I saw this post. Great solution and I used it several times already.

Wu Chia Chong
11 years ago

Is this also known as “Prototype” pattern in GO4?
I guess this is good for “Audit Trail” purpose for Enterprise applications.

Rene Enriquez
10 years ago
Reply to  Wu Chia Chong

Yes, is an implementation example of prototype pattern