Following example explains how to use BeanUtils to copy java bean data in various scenarios

Create Source Bean

AddressType.java
package com.codesimplify.beanutils.types;

public class AddressType {

	private String addrLine1;
	private String addrLine2;
	private String city;
	private String state;
	private String zipCode;
	public String getAddrLine1() {
		return addrLine1;
	}
	public void setAddrLine1(String addrLine1) {
		this.addrLine1 = addrLine1;
	}
	public String getAddrLine2() {
		return addrLine2;
	}
	public void setAddrLine2(String addrLine2) {
		this.addrLine2 = addrLine2;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getZipCode() {
		return zipCode;
	}
	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}
	@Override
	public String toString() {
		return "AddressType [addrLine1=" + addrLine1 + ", addrLine2=" + addrLine2 + ", city=" + city + ", state="
				+ state + ", zipCode=" + zipCode + "]";
	}
	
}

Create Target Bean

Target bean created is identical to source bean except there is one data type difference ( zipCode field ). zipCode in source bean is declared as String Type but in target bean it is defined as int.

AddressBean.java
package com.codesimplify.beanutils.beans;

public class AddressBean {

	private String addrLine1;
	private String addrLine2;
	private String city; 
	private String state;
	private int zipCode;
	public String getAddrLine1() {
		return addrLine1;
	}
	public void setAddrLine1(String addrLine1) {
		this.addrLine1 = addrLine1;
	}
	public String getAddrLine2() {
		return addrLine2;
	}
	public void setAddrLine2(String addrLine2) {
		this.addrLine2 = addrLine2;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public int getZipCode() {
		return zipCode;
	}
	public void setZipCode(int zipCode) {
		this.zipCode = zipCode;
	}
	@Override
	public String toString() {
		return "AddressBean [addrLine1=" + addrLine1 + ", addrLine2=" + addrLine2 + ", city=" + city + ", state="
				+ state + ", zipCode=" + zipCode + "]";
	}
	
}

BeanUtils provides 2 options to copy data from source bean to target bean.

  • 1 step Approach- Use BeanUtils.copyProperties()
  • 2 step Approach – Use BeanUtils.describe() and BeanUtils.populate()

1. Convert Source bean to Target Bean using BeanUtils.copyProperties() method

	AddressType addrType=new AddressType();
	addrType.setAddrLine1("101 Howard Lane");
	addrType.setAddrLine2("APT 222");
	addrType.setCity("Austin");
	addrType.setState("Florida");
	addrType.setZipCode("32050");
	AddressBean destAddrBean=new AddressBean();

        BeanUtils.copyProperties(destAddrBean, addrType);
	System.out.println(targetBean);
       // Output: AddressBean [addrLine1=101 Howard Lane, addrLine2=APT 222, city=Austin, state=Florida, zipCode=32050]
If you observe zipcode, in source bean it is defined as String type and in target bean, it is defined as int. BeanUtils converted zipCode from String to int.

2. Use 2 step Approach: BeanUtils describe() and populate() method usage

2.1 Extract Source Bean data into Map using describe()

	Map beanProperties = BeanUtils.describe(addrType);

2.2. Populate bean with extracted data in Map using populate()

	Map beanProperties = BeanUtils.describe(addrType);
	BeanUtils.populate(destAddrBean, beanProperties);
      //Output: AddressBean [addrLine1=101 Howard Lane, addrLine2=APT 222, city=Austin, state=Florida, zipCode=32050]
Using 2 step approach ( extract and populate) facilitates to skip/manipulate the extracted data before populating into target bean.

References:

Apache Commons BeanUtils Guide

Java Object copy using BeanUtils
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *