How do I copy properties from one bean to another?
Author: Deron Eriksson
Description: This Java tutorial describes how to copy properties from one JavaBean to another.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1
The Jakarta Commons BeanUtilsS library features a variety of functionality for working with JavaBeansW. The 1.7.0 version of BeanUtils has a dependency on the CommonsSW Logging library. Additionally, in this tutorial, I make use of the ToStringBuilder in the Commons LangS library to output the properties of the JavaBeans in this example. The jarW files are added to my lib/ directory and added to my project's build path, as shown below. This project features two beans. The first bean, FromBean, has three properties: name, aProp, and bProp. FromBean.javapackage test; import java.io.Serializable; public class FromBean implements Serializable { private static final long serialVersionUID = 1L; private String name; private String aProp; private String bProp; public FromBean() { } public FromBean(String name, String aProp, String bProp) { this.name = name; this.aProp = aProp; this.bProp = bProp; } public String getAProp() { return aProp; } public void setAProp(String prop) { aProp = prop; } public String getBProp() { return bProp; } public void setBProp(String prop) { bProp = prop; } public String getName() { return name; } public void setName(String name) { this.name = name; } } The second bean, ToBean, has three properties: name, bProp, and cProp. ToBean.javapackage test; import java.io.Serializable; public class ToBean implements Serializable { private static final long serialVersionUID = 1L; private String name; private String bProp; private String cProp; public ToBean() { } public ToBean(String name, String bProp, String cProp) { this.name = name; this.bProp = bProp; this.cProp = cProp; } public String getBProp() { return bProp; } public void setBProp(String prop) { bProp = prop; } public String getCProp() { return cProp; } public void setCProp(String prop) { cProp = prop; } public String getName() { return name; } public void setName(String name) { this.name = name; } } The BeanUtilsCopyPropertiesTest class will let us test copying properties from one bean to another. First, it creates a fromBean object and a toBean object. It outputs these objects to the console. Then, it call the BeanUtils.copyProperties() method with toBean as the first parameter and fromBean as the second parameter. Notice that the object that is copied to is the first parameter and the object that is copied from is the second parameter. In my opinion, the ordering of the parameters is a little backwards, but the person who wrote this method probably came from a C background and as a result was comfortable with this ordering. Following the copyProperties call, the object properties of fromBean and toBean are output to the console. BeanUtilsCopyPropertiesTest.javapackage test; import java.lang.reflect.InvocationTargetException; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.lang.builder.ToStringBuilder; public class BeanUtilsCopyPropertiesTest { public static void main(String[] args) { FromBean fromBean = new FromBean("fromBean", "fromBeanAProp", "fromBeanBProp"); ToBean toBean = new ToBean("toBean", "toBeanBProp", "toBeanCProp"); System.out.println(ToStringBuilder.reflectionToString(fromBean)); System.out.println(ToStringBuilder.reflectionToString(toBean)); try { System.out.println("Copying properties from fromBean to toBean"); BeanUtils.copyProperties(toBean, fromBean); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } System.out.println(ToStringBuilder.reflectionToString(fromBean)); System.out.println(ToStringBuilder.reflectionToString(toBean)); } } (Continued on page 2) Related Tutorials:
|