Most Java developers are used to creating Java classes that conform to the JavaBeans naming patterns for property getters and setters. It is natural to then access these methods directly, using calls to the corresponding getXxx and setXxx methods.
However, there are some occasions where dynamic access to Java object properties (without compiled-in knowledge of the property getter and setter methods to be called) is needed.
Example use cases include:
- Building scripting languages that interact with the Java object model (such as the Bean Scripting Framework).
- Building template language processors for web presentation and similar uses (such as JSP or Velocity).
- Building custom tag libraries for JSP and XSP environments (such as Apache Taglibs, Struts, Cocoon).
- Consuming XML-based configuration resources (such as Ant build scripts, web application deployment descriptors, Tomcat’s server.xml file).
The Jakarta Commons BeanUtils
library features a variety of functionality for working with JavaBeans. The 1.8.3 version of BeanUtils has a dependency on the CommonsLogging library
.
Additionally, in this tutorial, I have used ToStringBuilder
in the Commons Lang library to output the properties of the JavaBeans in this example.
Another must read:
- How to Generate Java Thread Dump Programmatically
- How to Override equals() and hashcode() Method in Java
This is a simple Java Example which demonstrate the way to copy properties from One Bean to Another.
CrunchifyBeanCopyExample.java
package crunchify.com.tutorial; import java.lang.reflect.InvocationTargetException; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.lang.builder.ToStringBuilder; /** * @author Crunchify.com * */ public class CrunchifyBeanCopyExample { public static void main(String[] args) { CrunchifyBeanCopyFrom bean1 = new CrunchifyBeanCopyFrom("Crunchify", "Online Java and WordPress Tutorials"); CrunchifyBeanCopyTo bean2 = new CrunchifyBeanCopyTo("Paypal", "Financial Place"); System.out.println(ToStringBuilder.reflectionToString(bean1)); System.out.println(ToStringBuilder.reflectionToString(bean2)); try { System.out.println("\nCopying properties from fromBean to toBean...\n"); BeanUtils.copyProperties(bean2, bean1); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } System.out.println(ToStringBuilder.reflectionToString(bean1)); System.out.println(ToStringBuilder.reflectionToString(bean2)); } }
CrunchifyBeanCopyFrom.java
package com.crunchify.tutorials; import java.io.Serializable; /** * @author Crunchify.com * */ @SuppressWarnings("serial") public class CrunchifyBeanCopyFrom implements Serializable { private String name; private String myProp; public CrunchifyBeanCopyFrom() { } public CrunchifyBeanCopyFrom(String name, String myProp) { this.name = name; this.myProp = myProp; } public String getMyProp() { return myProp; } public void setMyProp(String prop) { myProp = prop; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
CrunchifyBeanCopyTo.java
package com.crunchify.tutorials; import java.io.Serializable; /** * @author Crunchify.com * */ @SuppressWarnings("serial") public class CrunchifyBeanCopyTo implements Serializable { private String name; private String myProp; public CrunchifyBeanCopyTo() { } public CrunchifyBeanCopyTo(String name, String myProp) { this.name = name; this.myProp = myProp; } public String getMyProp() { return myProp; } public void setMyProp(String prop) { myProp = prop; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Output:
crunchify.com.tutorial.CrunchifyBeanCopyFrom@58fe210a[name=Crunchify,myProp=Online Java and WordPress Tutorials] crunchify.com.tutorial.CrunchifyBeanCopyTo@66780515[name=Paypal,myProp=Financial Place] Copying properties from fromBean to toBean... crunchify.com.tutorial.CrunchifyBeanCopyFrom@58fe210a[name=Crunchify,myProp=Online Java and WordPress Tutorials] crunchify.com.tutorial.CrunchifyBeanCopyTo@66780515[name=Crunchify,myProp=Online Java and WordPress Tutorials]
Here are maven dependencies:
Apache Common Beanutils Maven dependency
<dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency>
Apache Commons Logging Maven dependency
<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency>