Close

Spring Framework - BeanWrapper Examples

Spring Framework 

Using overloaded methods: BeanWrapper.setPropertyValue.

package com.logicbig.example.beanwrapper;

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

public class BeanWrapperExample {
public static void main (String[] args) {
BeanWrapper bw = new BeanWrapperImpl(new TestBean());
bw.setPropertyValue("aString", "someString");
PropertyValue pv = new PropertyValue("anInt", 3);
//the next commented line will also work
/*PropertyValue pv = new PropertyValue("anInt", "3");*/
bw.setPropertyValue(pv);
System.out.println(bw.getWrappedInstance());

}
}

Output

TestBean{aString='someString', anInt=3, date=Sat Aug 05 02:56:39 CDT 2017}




Setting properties from a map.

package com.logicbig.example.beanwrapper;

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

import java.util.HashMap;
import java.util.Map;


public class BeanWrapperMapExample {
public static void main (String[] args) {
BeanWrapperImpl bw = new BeanWrapperImpl(new TestBean());

Map<String, Object> map = new HashMap<>();
map.put("aString", "SomeString");
map.put("anInt", "10");
bw.setPropertyValues(map);
System.out.println(bw.getWrappedInstance());
}
}

Output

TestBean{aString='SomeString', anInt=10, date=Mon May 01 16:08:41 CDT 2017}




MutablePropertyValues example.

package com.logicbig.example.beanwrapper;

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

public class BeanWrapperMutablePropertyExample {
public static void main (String[] args) {
BeanWrapper bw = new BeanWrapperImpl(new TestBean());
MutablePropertyValues mpv = new MutablePropertyValues();
mpv.add("aString", "SomeString");
mpv.add("anInt", "10");

bw.setPropertyValues(mpv);
System.out.println(bw.getWrappedInstance());
}
}

Output

TestBean{aString='SomeString', anInt=10, date=Mon May 01 16:08:43 CDT 2017}




package com.logicbig.example.beanwrapper;

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

import java.beans.PropertyDescriptor;

public class BeanWrapperPropertyDescriptorExample {
public static void main (String[] args) {


BeanWrapper bw = new BeanWrapperImpl(new TestBean());
bw.setPropertyValue("aString", "someString");
PropertyValue pv = new PropertyValue("anInt", "3");
bw.setPropertyValue(pv);
System.out.println(bw.getWrappedInstance());
for (PropertyDescriptor pd : bw.getPropertyDescriptors()) {
System.out.println(pd.getDisplayName() + " " + pd.getPropertyType());
}
}
}

Checking if the property is ready only.

package com.logicbig.example.beanwrapper;

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

public class BeanWrapperReadOnlyCheckExample {
public static void main (String[] args) {
BeanWrapper bw = new BeanWrapperImpl(new TestBean());
bw.setPropertyValue("aString", "someString");
System.out.println("date read only: " + bw.isReadableProperty("date"));
//similarly we have bw.isWritableProperty(..) method
System.out.println(bw.getWrappedInstance());
}
}

Output

date read only: true
TestBean{aString='someString', anInt=5, date=Mon May 01 16:08:53 CDT 2017}




Setting nested properties.

package com.logicbig.example.beanwrapper;

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

public class BeanWrapperNestedBean {
public static void main (String[] args) {
BeanWrapper bw2 = new BeanWrapperImpl(new TestBean2());
bw2.setPropertyValue("anotherString", "stringVal");

BeanWrapper bw = new BeanWrapperImpl(new TestBean());
bw.setPropertyValue("anInt", 3);
bw.setPropertyValue("aString", "aStrVal");

bw2.setPropertyValue("testBean", bw.getWrappedInstance());
System.out.println(bw2.getWrappedInstance());
}
}

Output

TestBean2{testBean=TestBean{aString='aStrVal', anInt=3, date=Mon May 01 16:08:45 CDT 2017}, anotherString='stringVal'}




Setting nested properties

package com.logicbig.example.beanwrapper;

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

public class BeanWrapperNestedBean2 {
public static void main (String[] args) {
TestBean2 testBean2 = new TestBean2();
testBean2.setTestBean(new TestBean());

BeanWrapper bw2 = new BeanWrapperImpl(testBean2);
bw2.setPropertyValue("anotherString", "stringVal");
bw2.setPropertyValue("testBean.aString", "aStr");
bw2.setPropertyValue("testBean.anInt", 7);

System.out.println(bw2.getWrappedInstance());

}
}

Output

TestBean2{testBean=TestBean{aString='aStr', anInt=7, date=Mon May 01 16:08:47 CDT 2017}, anotherString='stringVal'}

Setting indexed properties

package com.logicbig.example.beanwrapper;

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

import java.util.ArrayList;

public class BeanWrapperNestedIndexProperty {
public static void main (String[] args) {
TestBean3 testBean = new TestBean3();
testBean.setTestBeans(new ArrayList<>());
testBean.setTestBeans(0, new TestBean());
testBean.setTestBeans(1, new TestBean());

BeanWrapper bw2 = new BeanWrapperImpl(testBean);
bw2.setPropertyValue("testBeans[0].aString", "aStr0");
bw2.setPropertyValue("testBeans[0].anInt", "3");

bw2.setPropertyValue("testBeans[1].aString", "aStr1");
bw2.setPropertyValue("testBeans[1].anInt", "6");
System.out.println(bw2.getWrappedInstance());
}
}

Output

TestBean3{testBeans=[TestBean{aString='aStr0', anInt=3, date=Mon May 01 16:08:49 CDT 2017}, TestBean{aString='aStr1', anInt=6, date=Mon May 01 16:08:49 CDT 2017}]}




BeanWrapper throws org.springframework.beans.TypeMismatchException exception if input cannot be converted to the target type.

package com.logicbig.example;


import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.MutablePropertyValues;

public class BeanWrapperWithError {
public static void main (String[] args) {
MutablePropertyValues mpv = new MutablePropertyValues();
mpv.add("anInt", "10x"); // 10x cannot be converted to int type

BeanWrapper bw = new BeanWrapperImpl(new TestBean());
bw.setPropertyValues(mpv);
System.out.println(bw.getWrappedInstance());
}
}
Original Post




Setting nested properties.

package com.logicbig.example;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

public class BeanWrapperNestedBean {
public static void main (String[] args) {
BeanWrapper beanWrapperEmployee = new BeanWrapperImpl(new Employee());
beanWrapperEmployee.setPropertyValue("name", "Joe");

BeanWrapper beanWrapperDept = new BeanWrapperImpl(new Department());
beanWrapperDept.setPropertyValue("deptCode", 1101);
beanWrapperDept.setPropertyValue("deptName", "IT");

beanWrapperEmployee.setPropertyValue("department", beanWrapperDept.getWrappedInstance());
System.out.println(beanWrapperEmployee.getWrappedInstance());
}
}

Output

Employee{name='Joe', department=Department{deptCode='1101', deptName='IT'}}
Original Post

Setting nested properties

package com.logicbig.example;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

public class BeanWrapperNestedBean2 {
public static void main (String[] args) {
BeanWrapper bw = new BeanWrapperImpl(new Employee());
bw.setPropertyValue("name", "Joe");
bw.setPropertyValue("department", new Department());
bw.setPropertyValue("department.deptCode", 1001);
bw.setPropertyValue("department.deptName", "IT");
System.out.println(bw.getWrappedInstance());
}
}

Output

Employee{name='Joe', department=Department{deptCode='1001', deptName='IT'}}
Original Post




Setting indexed properties

package com.logicbig.example;

import org.springframework.beans.BeanWrapperImpl;
import java.util.ArrayList;
import java.util.HashMap;

public class BeanWrapperNestedIndexProperty {
public static void main(String[] args) {
BeanWrapperImpl bw = new BeanWrapperImpl(new EmployeeGroup());
bw.setPropertyValue("groupName", "TennisTeam");
bw.setPropertyValue("employeeList", new ArrayList<>());

bw.setPropertyValue("employeeList[0]", new Employee());
bw.setPropertyValue("employeeList[0].name", "Mike");
bw.setPropertyValue("employeeList[0].department", new Department());
bw.setPropertyValue("employeeList[0].department.deptCode", 1001);
bw.setPropertyValue("employeeList[0].department.deptName", "Admin");

bw.setPropertyValue("employeeList[1]", new Employee());
bw.setPropertyValue("employeeList[1].name", "Jackie");
bw.setPropertyValue("employeeList[1].department", new Department());
bw.setPropertyValue("employeeList[1].department.deptCode", 1002);
bw.setPropertyValue("employeeList[1].department.deptName", "Account");

bw.setPropertyValue("groupProperties", new HashMap<>());
bw.setPropertyValue("groupProperties[location]", "Texas");
bw.setPropertyValue("groupProperties[matchesPerYear]", "2");

System.out.println(bw.getWrappedInstance());

System.out.println("-- accessing field values --");
Object employeeList = bw.getPropertyValue("employeeList");
System.out.println("employeeList: " + employeeList);
Object firstEmployee = bw.getPropertyValue("employeeList[0]");
System.out.println("employeeList[0]: " + firstEmployee);
Object firstEmployeeDept = bw.getPropertyValue("employeeList[0].department.deptName");
System.out.println("employeeList[0].department.deptName: " + firstEmployeeDept);
Object matchesPerYear = bw.getPropertyValue("groupProperties[matchesPerYear]");
System.out.println("groupProperties[matchesPerYear]: "+matchesPerYear);
}
}

Output

EmployeeGroup{groupName='TennisTeam', employeeList=[Employee{name='Mike', department=Department{deptCode='1001', deptName='Admin'}}, Employee{name='Jackie', department=Department{deptCode='1002', deptName='Account'}}], groupProperties={location=Texas, matchesPerYear=2}}
-- accessing field values --
employeeList: [Employee{name='Mike', department=Department{deptCode='1001', deptName='Admin'}}, Employee{name='Jackie', department=Department{deptCode='1002', deptName='Account'}}]
employeeList[0]: Employee{name='Mike', department=Department{deptCode='1001', deptName='Admin'}}
employeeList[0].department.deptName: Admin
groupProperties[matchesPerYear]: 2
Original Post




package com.logicbig.example;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import java.beans.PropertyDescriptor;

public class BeanWrapperPropertyDescriptorExample {
public static void main (String[] args) {
BeanWrapper bw = new BeanWrapperImpl(new Person());
printPropertyDescriptor(bw.getPropertyDescriptors());
}

private static void printPropertyDescriptor(PropertyDescriptor[] descriptors) {
for (PropertyDescriptor descriptor : descriptors) {
System.out.println("Name: "+descriptor.getName());
System.out.println("Reader: "+descriptor.getReadMethod());
System.out.println("Writer: "+descriptor.getWriteMethod());
System.out.println("------");
}
}
}
Original Post

Using overloaded methods: BeanWrapper.setPropertyValue.

package com.logicbig.example;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.PropertyValue;

public class BeanWrapperExample {
public static void main (String[] args) {
BeanWrapper beanWrapper = new BeanWrapperImpl(new Person());
beanWrapper.setPropertyValue("name", "John");
beanWrapper.setPropertyValue("age", 33);
//the next commented line will also work, auto conversion is performed by Spring
/* beanWrapper.setPropertyValue("age", "33");*/

System.out.println("bean: "+beanWrapper.getWrappedInstance());
//getting property value
Object value = beanWrapper.getPropertyValue("name");
System.out.println("person.name: "+value);
}
}

Output

bean: Person{name='John', age=33}
person.name: John
Original Post




MutablePropertyValues example.

package com.logicbig.example;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.MutablePropertyValues;

public class BeanWrapperMutablePropertyExample {
public static void main (String[] args) {
BeanWrapper bw = new BeanWrapperImpl(new Person());
MutablePropertyValues mpv = new MutablePropertyValues();
mpv.add("name", "Diana");
mpv.add("age", "30");

bw.setPropertyValues(mpv);
System.out.println(bw.getWrappedInstance());
}
}

Output

Person{name='Diana', age=30}
Original Post




Checking if the property is ready only.

package com.logicbig.example;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

public class BeanWrapperReadOnlyCheckExample {
public static void main (String[] args) {
BeanWrapper bw = new BeanWrapperImpl(new TestBean());
bw.setPropertyValue("aString", "someString");
System.out.println("date read only: " + bw.isReadableProperty("date"));
//similarly we have bw.isWritableProperty(..) method
System.out.println(bw.getWrappedInstance());
}
}

Output

date read only: false
TestBean{aString='someString', anInt=5, date=Tue May 19 16:20:00 CDT 1970}
Original Post




Setting properties from a map.

package com.logicbig.example;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import java.util.HashMap;
import java.util.Map;


public class BeanWrapperMapExample {
public static void main (String[] args) {
BeanWrapper beanWrapper = new BeanWrapperImpl(new Person());
Map<String, Object> map = new HashMap<>();
map.put("name", "Tina");
map.put("age", "32");
beanWrapper.setPropertyValues(map);
System.out.println(beanWrapper.getWrappedInstance());
}
}

Output

Person{name='Tina', age=32}
Original Post




Using overloaded methods: BeanWrapper.setPropertyValue.

package com.logicbig.example;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.PropertyValue;

public class BeanWrapperPropertyValueExample {
public static void main (String[] args) {
BeanWrapper beanWrapper = new BeanWrapperImpl(new Person());
PropertyValue pv = new PropertyValue("name", "John");
beanWrapper.setPropertyValue("name", "John");
PropertyValue pv2 = new PropertyValue("age", 33);
beanWrapper.setPropertyValue(pv);
beanWrapper.setPropertyValue(pv2);
System.out.println("bean: "+beanWrapper.getWrappedInstance());
}
}

Output

bean: Person{name='John', age=33}
Original Post




Using overloaded methods: BeanWrapper.setPropertyValue.

package com.logicbig.example;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

public class BeanWrapperExample {
public static void main (String[] args) {
BeanWrapper beanWrapper = new BeanWrapperImpl(new Person());
beanWrapper.setPropertyValue("name", "John");
beanWrapper.setPropertyValue("age", "33");
//the next commented line will also work, auto conversion is performed by Spring
/* beanWrapper.setPropertyValue("age", "33");*/

System.out.println("bean: "+beanWrapper.getWrappedInstance());
//getting property value
Object value = beanWrapper.getPropertyValue("name");
System.out.println("person.name: "+value);
}
}

Output

bean: Person{name='John', age=33}
person.name: John




In this standalone (without spring context) example, we are using BeanWrapperImpl to access default property editors.

package com.logicbig.example;

import org.springframework.beans.BeanWrapperImpl;
import java.beans.PropertyEditor;
import java.util.Currency;

public class PropertyEditorAccessExample {

public static void main (String[] args) {
BeanWrapperImpl wrapper = new BeanWrapperImpl();
PropertyEditor editor = wrapper.getDefaultEditor(Currency.class);
editor.setAsText("MYR");
Currency value = (Currency) editor.getValue();
System.out.println(value.getDisplayName());
}
}

Output

Malaysian Ringgit
Original Post




String auto conversion to Date

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import java.time.ZoneId;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;

public class PersonBeanWrapperExample {
public static void main(String[] args) {
Person person = new Person();
BeanWrapper bw = new BeanWrapperImpl(person);
bw.setPropertyValue("name", "Tim");
bw.setPropertyValue("dateOfBirth", "Thu Jul 01 16:18:19 CDT 2021");
System.out.println(person);
System.out.println(person.getDateOfBirth().toInstant()
.atZone(ZoneId.systemDefault()).toLocalDate());
}
}

Output

Person{name='Tim', dateOfBirth=Thu Jul 01 16:18:19 CDT 2021}
2021-07-01
Original Post




In this standalone example, we are using BeanWrapperImpl to register custom editor

import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PersonBeanWrapperExample2 {
public static void main(String[] args) {
Person person = new Person();
BeanWrapperImpl beanWrapper= new BeanWrapperImpl(person);

CustomDateEditor dateEditor = new CustomDateEditor(
new SimpleDateFormat("yyyy-MM-dd"), true);
beanWrapper.registerCustomEditor(Date.class, dateEditor);

beanWrapper.setPropertyValue("name", "Tim");
beanWrapper.setPropertyValue("dateOfBirth", "1984-07-10");

System.out.println(person);
}
}

Output

Person{name='Tim', dateOfBirth=Tue Jul 10 00:00:00 CDT 1984}
Original Post




In this example, we are creating our own PropertyEditor and registering it with BeanWrapperImpl.

import org.springframework.beans.BeanWrapperImpl;
import java.beans.PropertyEditorSupport;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class LocalDateEditorExample {

public static void main(String[] args) {
Person person = new Person();
BeanWrapperImpl bw = new BeanWrapperImpl(person);
bw.registerCustomEditor(LocalDate.class,
new LocalDateEditor(DateTimeFormatter.ofPattern("yyyy/MM/dd")));
bw.setPropertyValue("name", "Tim");
bw.setPropertyValue("dateOfBirth", "1984/07/06");
System.out.println(person);
}

private static class Person{
private String name;
private LocalDate dateOfBirth;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public LocalDate getDateOfBirth() {
return dateOfBirth;
}

public void setDateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", dateOfBirth=" + dateOfBirth +
'}';
}
}

public static class LocalDateEditor extends PropertyEditorSupport {
private final DateTimeFormatter formatter;

public LocalDateEditor(DateTimeFormatter formatter) {
this.formatter = formatter;
}

@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(LocalDate.parse(text, formatter));
}

@Override
public String getAsText() {
return getValue().toString();
}
}
}

Output

Person{name='Tim', dateOfBirth=1984-07-06}
Original Post




See Also