Close

Spring Framework - BeanWrapper Examples

Spring Framework 

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

package com.logicbig.example;

import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyEditor;
import java.util.Currency;

public class DefaultEditorsTest {

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




This example shows how to use a desktop application like JavaFX for fields input conversion to java object by using Spring property editors. It initializes the target bean 'LocaleClientBean' properties by using BeanWrapperImpl.

package com.logicbig.example;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyEditor;
import java.util.Locale;

public class JavaFxDefaultEditorExample extends Application {


public static void main (String[] args) {
launch(args);
}

@Override
public void start (Stage stage) throws Exception {
Scene scene = new Scene(new Group(), 450, 250);

TextField localeField = new TextField();

GridPane grid = new GridPane();
grid.setVgap(4);
grid.setHgap(10);
grid.setPadding(new Insets(5, 5, 5, 5));
grid.add(new Label("Enter locale: "), 0, 0);
grid.add(localeField, 1, 0);

Button button = new Button("Submit");
grid.add(button, 2, 0);

Label outputLabel = new Label();
outputLabel.setTextFill(Color.BLUE);

grid.add(outputLabel, 1, 1);

button.setOnAction(event -> {
LocaleClientBean bean = new LocaleClientBean();

System.out.println(localeField.getText());
BeanWrapperImpl beanWrapper = new BeanWrapperImpl(bean);

//spring uses default property editor 'LocaleEditor' for conversion
beanWrapper.setPropertyValue("locale", localeField.getText().trim());

bean.workWithLocale();
outputLabel.setText(bean.getLocale()
.getDisplayCountry());


});

Group root = (Group) scene.getRoot();
root.getChildren()
.add(grid);
stage.setScene(scene);
stage.show();
}

public static class LocaleClientBean {
private Locale locale;

public void workWithLocale () {
System.out.println(locale.getLanguage());
}

public Locale getLocale () {
return locale;
}

public void setLocale (Locale locale) {
this.locale = locale;
}
}
}
Original Post




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;

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");

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);
}
}

Output

EmployeeGroup{groupName='TennisTeam', employeeList=[Employee{name='Mike', department=Department{deptCode='1001', deptName='Admin'}}, Employee{name='Jackie', department=Department{deptCode='1002', deptName='Account'}}]}
-- 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
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




See Also