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}
Original Post




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}
Original Post

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}
Original Post




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());
}
}
}
Original Post




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}
Original Post

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'}
Original Post




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'}
Original Post




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}]}
Original Post

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




See Also