Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Mastering Spring BeanWrapper: Set, Get, and Convert Bean Properties Efficiently

Mastering Spring BeanWrapper: Set, Get, and Convert Bean Properties Efficiently

This article introduces Spring's BeanWrapper, explains how to set and retrieve simple and nested properties, demonstrates indexed and key‑based access, covers automatic and custom type conversion using PropertyEditorSupport and Converter, and clarifies conversion priority and registration details for backend developers.

Environment: Spring 6.1.2

1. Introduction

BeanWrapper interface and BeanWrapperImpl implementation are important in Spring. They allow setting and getting property values (individually or in bulk), retrieving property descriptors, checking readability/writability, supporting nested properties, adding PropertyChangeListeners and VetoableChangeListeners without modifying target classes, and handling indexed properties. BeanWrapper is usually used by DataBinder and BeanFactory rather than directly by application code.

2. Practical Examples

2.1 Setting and Getting Basic and Nested Properties

Properties are accessed via setPropertyValue and getPropertyValue. Common expression forms include: name – maps to getName()/setName(...) account.name – nested property account[2] – indexed property (array, List, etc.) account[companyname] – Map key access

Example classes:

public class Company {
    private String name;
    private Employee employee;
    // getters and setters
}
public class Employee {
    private String name;
    private float salary;
    private String[] address = new String[2];
    private Map<String, String> account = new HashMap<>();
    // getters and setters
}

Manipulating these beans:

BeanWrapper company = new BeanWrapperImpl(new Company());
// set simple property
company.setPropertyValue("name", "Pack Company Inc.");
// create employee and bind
Employee emp = new Employee();
BeanWrapper empWrapper = new BeanWrapperImpl(emp);
empWrapper.setPropertyValue("name", "张三");
empWrapper.setPropertyValue("salary", 66666.66f);
company.setPropertyValue("employee", empWrapper.getWrappedInstance());
// get nested property
Float salary = (Float) company.getPropertyValue("employee.salary");
System.out.println(salary);
System.out.println(company.getWrappedInstance());

Output:

66666.66
Company[name=Pack Company Inc., employee=Employee[name=张三, salary=66666.66]]

2.2 Index Access

Employee emp = new Employee();
BeanWrapper empWrapper = new BeanWrapperImpl(emp);
empWrapper.setPropertyValue("address[0]", "新疆");
empWrapper.setPropertyValue("address[1]", "重庆");

2.3 Key Access

BeanWrapper empWrapper = new BeanWrapperImpl(Employee.class);
empWrapper.setPropertyValue("account[home]", "H0001");
empWrapper.setPropertyValue("account[work]", "W0001");
System.out.println(empWrapper.getWrappedInstance());

Output:

Employee[name=null, salary=0.0, address=[null, null], account={work=W0001, home=H0001}]

2.4 Automatic Type Conversion

When a string is assigned to a property of type float, BeanWrapper automatically converts it:

BeanWrapper empWrapper = new BeanWrapperImpl(Employee.class);
empWrapper.setPropertyValue("salary", "88888.88");

Spring loads all *Editor classes from org.springframework.beans.propertyeditors, e.g.,

this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true));

Default supported conversions are illustrated below:

Default type conversion table
Default type conversion table

2.5 Custom Type Conversion

Custom converters can be added via PropertyEditorSupport or Spring’s Converter interface.

Using PropertyEditorSupport:

public class StringToUserEditor extends PropertyEditorSupport {
    @Override
    public void setAsText(String text) {
        User user = new User();
        String[] infos = text.split(",");
        user.setAge(Integer.parseInt(infos[0]));
        user.setName(infos[1]);
        setValue(user);
    }
}

Register and use:

BeanWrapper wrapper = new BeanWrapperImpl(Pack.class);
wrapper.registerCustomEditor(User.class, new StringToUserEditor());
wrapper.setPropertyValue("user", "23,张三");
System.out.println(wrapper.getWrappedInstance());

Output:

Pack[user=User[age=23, name=张三]]

Using Spring’s Converter:

public class StringToUserConvert implements Converter<String, User> {
    @Override
    public User convert(String source) {
        User user = new User();
        String[] infos = source.split(",");
        user.setAge(Integer.parseInt(infos[0]));
        user.setName(infos[1] + " -");
        return user;
    }
}

Configure a ConversionService and apply:

BeanWrapper wrapper = new BeanWrapperImpl(Pack.class);
DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new StringToUserConvert());
wrapper.setConversionService(conversionService);
wrapper.setPropertyValue("user", "23,张三");
System.out.println(wrapper.getWrappedInstance());

Output:

Pack[user=User[age=23, name=张三 -]]

2.6 Conversion Priority

If both a custom PropertyEditor and a Converter are registered, the PropertyEditor takes precedence, as shown by the following test:

DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new StringToUserConvert());
wrapper.setConversionService(conversionService);
wrapper.registerCustomEditor(User.class, new StringToUserEditor());
// result: Pack[user=User[age=23, name=张三]]

Spring determines the conversion order based on internal rules, illustrated below:

Conversion order diagram
Conversion order diagram

Note: If a matching XxxEditor exists in the same package as the target type, Spring can locate it automatically without explicit registration.

Editor auto-discovery
Editor auto-discovery

Testing without registering a converter still produces correct output, demonstrating the robustness of BeanWrapper’s conversion mechanism.

That concludes the article; hope it helps you work with Spring BeanWrapper more elegantly.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
JavaBackend DevelopmentSpringType ConversionBeanWrapperPropertyEditor

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion