How To Use Reflection To Copy Properties From Pojo To Other Java Beans
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.(Like commons.logging-1.1.1.jar and commons-beanutils-1.8.3.jar) download
Create a new class “Person” in the package katik.test.form
package kartik.test.form;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Person {
private String name;
private int age;
private boolean flag;
private double number;
private BigDecimal big;
private List<String> aList;
private Map<String,List<String>> aMap=new HashMap<String, List<String>>();
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;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public double getNumber() {
return number;
}
public void setNumber(double number) {
this.number = number;
}
public BigDecimal getBig() {
return big;
}
public void setBig(BigDecimal big) {
this.big = big;
}
public List<String> getaList() {
return aList;
}
public void setaList(List<String> aList) {
this.aList = aList;
}
public Map<String, List<String>> getaMap() {
return aMap;
}
public void setaMap(Map<String, List<String>> aMap) {
this.aMap = aMap;
}
}
Create a new class “OtherPerson” in the package katik.test.to
package kartik.test.to;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class OtherPerson {
private String name;
private int age;
private boolean flag;
private double number;
private BigDecimal big;
private List<String> aList;
private Map<String,List<String>> aMap=new HashMap<String, List<String>>();
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;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public double getNumber() {
return number;
}
public void setNumber(double number) {
this.number = number;
}
public BigDecimal getBig() {
return big;
}
public void setBig(BigDecimal big) {
this.big = big;
}
public List<String> getaList() {
return aList;
}
public void setaList(List<String> aList) {
this.aList = aList;
}
public Map<String, List<String>> getaMap() {
return aMap;
}
public void setaMap(Map<String, List<String>> aMap) {
this.aMap = aMap;
}
}
3. Testing
Create a new class “Test” in the package katik.test.demo
package kartik.test.demo;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import kartik.test.from.Person;
import kartik.test.to.OtherPerson;
/**
* Class for test copy properties
* @author Kartik
* @date 18/12/2014
*/
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");
person.setFlag(true);
person.setNumber(10.1);
person.setBig(new BigDecimal(15.10));
//list set
List<String> list=new ArrayList<String>();
list.add("Kartik");
list.add("mandal");
person.setaList(list);
//map set
Map<String,List<String>> aMap=new HashMap<String,List<String>>();
aMap.put("1", list);
//list set
List<String> list2=new ArrayList<String>();
list2.add("Java");
list2.add("Jdk");
person.setaList(list2);
//map set
aMap.put("2", list2);
person.setaMap(aMap);// put all data in map
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(person.isFlag());
System.out.println("othePerson");
System.out.println(othePerson.getAge());
System.out.println(othePerson.getName());
BeanUtils.copyProperties(othePerson, person);
//this copy the data from one file to another file
System.out.println("*** After BeanUtils.copyProperties ***");
System.out.println("Person");
//othePerson.setAge(20);
System.out.println(person.getAge());
System.out.println(person.getName());
System.out.println(person.isFlag());
System.out.println("othePerson");
System.out.println(othePerson.getAge());
System.out.println(othePerson.getName());
System.out.println(othePerson.getNumber());
System.out.println(othePerson.getBig());
List<String> alst=othePerson.getaList();
for (String string : alst) {
System.out.println(string);
}
Map<String,List<String>> bMap=othePerson.getaMap();
for(String aKey : bMap.keySet()) {
List<String> aValue = bMap.get(aKey);
for (String string : aValue) {
System.out.println(string);
}
}
}
}
OutPut:
*** Before BeanUtils.copyProperties ***
Person
15
rene
true
othePerson
0
null
*** After BeanUtils.copyProperties ***
Person
15
rene
true
othePerson
15
rene
10.1
15.0999999999999996447286321199499070644378662109375
Java
Jdk
Java
Jdk
Kartik
mandal
1 comments:
Click here for commentsGreat Tutorial