Mastering Spring PropertyEditor: Custom Data Binding in Spring Boot
This tutorial explains how to use Java's PropertyEditor and PropertyEditorManager for custom String-to-object conversion, demonstrates registration in both non‑web and web Spring Boot environments, and provides practical code examples for seamless data binding.
1. Introduction
In a previous article we covered Spring data type conversion; this guide focuses on another way to achieve data binding using the PropertyEditor concept, which converts between String and custom objects such as dates.
2. PropertyEditor Conversion
The java.beans.PropertyEditor classes are part of the JDK and can be used in any Java project without additional dependencies.
2.1 Prepare Beans
<code>public class User {
private Integer age;
private String name;
// getters, setters
}
public class Dto {
private User user;
// getters, setters
}</code>2.2 Implement PropertyEditor
<code>public class UserPropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
String[] dataStr = text.split(",");
User user = new User();
user.setAge(Integer.valueOf(dataStr[0]));
user.setName(dataStr[1]);
setValue(user);
}
}</code>Usage example:
<code>UserPropertyEditor editor = new UserPropertyEditor();
editor.setAsText("12,张三");
User user = editor.getValue();</code>Registering with PropertyEditorManager for global reuse:
<code>PropertyEditorManager.registerEditor(User.class, UserPropertyEditor.class);
PropertyEditor editor = PropertyEditorManager.findEditor(User.class);
// beware of possible NPE
editor.setAsText("12,张三");</code>3. Integrating PropertyEditor with Spring
3.1 Non‑Web Environment
Define a BeanFactoryPostProcessor to register the custom editor:
<code>public class EditorBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.registerCustomEditor(User.class, UserEditor1.class);
}
}</code>Bind data manually:
<code>// obtain BeanFactory instance
ConfigurableListableBeanFactory beanFactory;
Dto target = new Dto();
DataBinder binder = new DataBinder(target);
beanFactory.copyRegisteredEditorsTo(binder);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("user", "22,张三");
binder.bind(pvs);
</code>3.2 Web Environment
Method 1 – @InitBinder
<code>@GetMapping("")
public Dto index(Dto dto) {
return dto;
}
@InitBinder
public void init(WebDataBinder binder) {
binder.registerCustomEditor(User.class, new UserPropertyEditor());
}
</code>You can place the @InitBinder method inside a class annotated with @ControllerAdvice to apply it globally.
Method 2 – Naming Convention
Simply place the custom editor class in the same package as the target type and name it UserEditor . Spring will automatically discover it.
<code>com
pack
domain
User
UserEditor
</code>File name must follow the pattern {TargetClass}Editor , e.g., UserEditor for the User type.
Both the PropertyEditor and PropertyEditorManager mechanisms work in Spring environments, providing a flexible way to handle custom type conversions.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.