Close

Spring Framework - BeanUtils Examples

Spring Framework 

Using BeanUtils.copyProperties for the same bean type.

package com.logicbig.example.beanutils;

import com.logicbig.example.TestBean;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

public class CopyPropsToSameType {
public static void main (String[] args) {
BeanWrapper bw = new BeanWrapperImpl(new TestBean());
bw.setPropertyValue("aString", "someString");
bw.setPropertyValue("anInt", 3);

TestBean testBean2 = new TestBean();

BeanUtils.copyProperties(bw.getWrappedInstance(), testBean2);

System.out.println(testBean2);
}
}

Output

TestBean{aString='someString', anInt=3, date=Mon May 01 16:08:07 CDT 2017}
Original Post




BeanUtils#copyProperties for different beans type example.

package com.logicbig.example.beanutils;

import com.logicbig.example.TestBean;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

public class CopyPropsToDifferentType {
public static void main (String[] args) {
BeanWrapper bw = new BeanWrapperImpl(new TestBean());
bw.setPropertyValue("aString", "someString");
bw.setPropertyValue("anInt", 3);

TestBeanDifferent testBean2 = new TestBeanDifferent();

//only properties of same name will be copied
BeanUtils.copyProperties(bw.getWrappedInstance(), testBean2);

System.out.println(testBean2);
}

private static class TestBeanDifferent {
private String aString;
private int differentInt;

public String getAString () {
return aString;
}

public void setAString (String aString) {
this.aString = aString;
}

public int getDifferentInt () {
return differentInt;
}

public void setDifferentInt (int differentInt) {
this.differentInt = differentInt;
}

@Override
public String toString () {
return "TestBeanDifferent{" +
"aString='" + aString + '\'' +
", differentInt=" + differentInt +
'}';
}
}
}

Output

TestBeanDifferent{aString='someString', differentInt=0}
Original Post




Using BeanUtils.getPropertyDescriptors and comparing with Introspector alternative.

package com.logicbig.example.beanutils;

import com.logicbig.example.TestBean;
import org.springframework.beans.BeanUtils;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.Arrays;

public class PropDescriptorExample {
public static void main (String[] args) throws IntrospectionException {
PropertyDescriptor[] actual = Introspector.getBeanInfo(TestBean.class)
.getPropertyDescriptors();
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(TestBean.class);

System.out.println(Arrays.toString(actual));
System.out.println(Arrays.toString(descriptors));
}
}

Output

[java.beans.PropertyDescriptor[name=AString; propertyType=class java.lang.String; readMethod=public java.lang.String com.logicbig.example.TestBean.getAString(); writeMethod=public void com.logicbig.example.TestBean.setAString(java.lang.String)], java.beans.PropertyDescriptor[name=anInt; propertyType=int; readMethod=public int com.logicbig.example.TestBean.getAnInt(); writeMethod=public void com.logicbig.example.TestBean.setAnInt(int)], java.beans.PropertyDescriptor[name=class; propertyType=class java.lang.Class; readMethod=public final native java.lang.Class java.lang.Object.getClass()], java.beans.PropertyDescriptor[name=date; propertyType=class java.util.Date; readMethod=public java.util.Date com.logicbig.example.TestBean.getDate()]]
Original Post




Using BeanUtils.resolveSignature example. Parsing string to do that.

package com.logicbig.example.beanutils;

import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ResolveSignature {

public static void main (String[] args) throws
InvocationTargetException, IllegalAccessException {
Method doSomething = BeanUtils.resolveSignature("aMethod(java.lang.String, int)",
LocalBean.class);
doSomething.invoke(new LocalBean(), "some string value", 100);

doSomething = BeanUtils.resolveSignature("aMethod(java.lang.Integer)",
LocalBean.class);
doSomething.invoke(new LocalBean(), 200);
}

private static class LocalBean {
public void aMethod (String str, int anInt) {
System.out.println(str);
System.out.println(anInt);
}

public void aMethod (Integer anInt) {
System.out.println(anInt);
}
}
}

Output

some string value
100
200
Original Post

Using BeanUtils.resolveSignature example. Resolving method with no arguments.

package com.logicbig.example.beanutils;

import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ResolveSignature2 {

public static void main (String[] args) throws
InvocationTargetException, IllegalAccessException {
Method doSomething = BeanUtils.resolveSignature("doSomething",
LocalBean.class);
doSomething.invoke(new LocalBean());
}

private static class LocalBean {
public void doSomething () {
System.out.println("-- doing something -- ");

}
}
}

Output

-- doing something -- 
Original Post




See Also