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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering Spring PropertyEditor: Custom Data Binding in Spring Boot

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

public class User {
  private Integer age;
  private String name;
  // getters, setters
}

public class Dto {
  private User user;
  // getters, setters
}

2.2 Implement PropertyEditor

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

Usage example:

UserPropertyEditor editor = new UserPropertyEditor();
editor.setAsText("12,张三");
User user = editor.getValue();

Registering with PropertyEditorManager for global reuse:

PropertyEditorManager.registerEditor(User.class, UserPropertyEditor.class);
PropertyEditor editor = PropertyEditorManager.findEditor(User.class);
// beware of possible NPE
editor.setAsText("12,张三");

3. Integrating PropertyEditor with Spring

3.1 Non‑Web Environment

Define a BeanFactoryPostProcessor to register the custom editor:

public class EditorBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    beanFactory.registerCustomEditor(User.class, UserEditor1.class);
  }
}

Bind data manually:

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

3.2 Web Environment

Method 1 – @InitBinder

@GetMapping("")
public Dto index(Dto dto) {
  return dto;
}

@InitBinder
public void init(WebDataBinder binder) {
  binder.registerCustomEditor(User.class, new UserPropertyEditor());
}

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.

com
  pack
    domain
      User
      UserEditor

File name must follow the pattern {TargetClass}Editor, e.g., UserEditor for the User type.

File structure illustration
File structure illustration

Both the PropertyEditor and PropertyEditorManager mechanisms work in Spring environments, providing a flexible way to handle custom type conversions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

backend-developmentSpring Bootdata bindingPropertyEditor
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.