import java.lang.reflect.InvocationTargetException;
public class BeanUtilCopyTest {
public static void main(String[] args) {
Source source = new Source();
source.setId("555");
source.setName("source");
source.setSourceName("sourcename");
Target target1 = new Target();
target1.setTargetName("target1");
Target target2 = new Target();
target2.setTargetName("target2");
Target target3 = new Target();
target3.setTargetName("target3");
System.out.println("source=" + source.toString());
System.out.println("target1=" + target1.toString());
org.springframework.beans.BeanUtils.copyProperties(source, target1);
System.out
.println("--org.springframework.beans.BeanUtils.copyProperties(source,target1)-前赋后--");
System.out.println("target1=" + target1.toString());
try {
System.out.println("target2=" + target2.toString());
org.apache.commons.beanutils.BeanUtils.copyProperties(target2,
source);
System.out
.println("--org.apache.commons.beanutils.BeanUtils.copyProperties(target2,source)--后赋前--");
System.out.println("target2=" + target2.toString());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
try {
System.out.println("target3=" + target3.toString());
org.apache.commons.beanutils.PropertyUtils.copyProperties(target3,
source);
System.out
.println("--org.apache.commons.beanutils.PropertyUtils.copyProperties(target3,source)--后赋前--");
System.out.println("target3=" + target3.toString());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
/**
* 有属性对象 (源对象)
*
* @author Administrator
*
*/
public static class Source {
private String id;
private String name;
private String sourceName;
private Integer age;
private Long timelLong;
private Boolean isFlag;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSourceName() {
return sourceName;
}
public void setSourceName(String sourceName) {
this.sourceName = sourceName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Long getTimelLong() {
return timelLong;
}
public void setTimelLong(Long timelLong) {
this.timelLong = timelLong;
}
public Boolean getIsFlag() {
return isFlag;
}
public void setIsFlag(Boolean isFlag) {
this.isFlag = isFlag;
}
@Override
public String toString() {
return "Source [id=" + id + ", name=" + name + ", sourceName="
+ sourceName + ", age=" + age + ", timelLong=" + timelLong
+ ", isFlag=" + isFlag + "]";
}
}
/**
* 空属性对象 (目标对象)
*
* @author Administrator
*
*/
public static class Target {
private String id;
private String name;
private String targetName;
private Integer age;
private Long timelLong;
private Boolean isFlag;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTargetName() {
return targetName;
}
public void setTargetName(String targetName) {
this.targetName = targetName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Long getTimelLong() {
return timelLong;
}
public void setTimelLong(Long timelLong) {
this.timelLong = timelLong;
}
public Boolean getIsFlag() {
return isFlag;
}
public void setIsFlag(Boolean isFlag) {
this.isFlag = isFlag;
}
@Override
public String toString() {
return "Target [id=" + id + ", name=" + name + ", targetName="
+ targetName + ", age=" + age + ", timelLong=" + timelLong
+ ", isFlag=" + isFlag + "]";
}
}
}