HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Spring Core / Spring Property Editor – CustomEditorConfigurer Example

Spring Property Editor – CustomEditorConfigurer Example

A property editor is a feature of the JavaBeans API for converting property values to and from text values. Each property editor is designed for a certain type of property only. You may wish to employ property editors to simplify your bean configurations. In this tutorial, we will learn to configure spring’s build-in CustomDateEditor class into your application.

CustomEditorConfigurer and CustomDateEditor configurations

Normally, you will register a property editor in the container before it may be used. The CustomEditorConfigurer class is implemented as a built-in bean factory post processor for you to register your custom property editors before any of the beans get instantiated.

Why CustomDateEditor

For example, in your application if you want to convert date values from string format to java.util.Date objects or vice-versa, you can use CustomDateEditor class. The CustomDateEditor class that comes with Spring is for converting date strings into java.util.Date properties.

A CustomEditorConfigurer bean can be declared into application context as below:

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
	<property name="propertyEditorRegistrars">
		<list>
			<bean class="com.howtodoinjava.demo.processors.CustomDateEditorRegistrar" />
		</list>
	</property>
</bean>

Register/Configure CustomDateEditor

CustomDateEditorRegistrar class should be declared in below manner from spring 4.x onwards.

public class CustomDateEditorRegistrar implements PropertyEditorRegistrar 
{
    public void registerCustomEditors(PropertyEditorRegistry registry) 
	{
        registry.registerCustomEditor(Date.class, 
				new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
    }
}

CustomDateEditor Demo

Now everytime, when you pass a bean property value (of type java.util.Date) in string format e.g. 2007-09-30, it will be automatically converted to Date object.

Let’s Test the configuration. To test, I have created a EmployeeDTO bean having one date field as dateOfBirth.

public class EmployeeDTO {
	
	private Integer id;
	private String firstName;
	private String lastName;
	private String designation;
	private Date dateOfBirth;

	//Setters and Getters

	@Override
	public String toString() {
		return "EmployeeDTO [id=" + id + ", firstName=" + firstName
				+ ", lastName=" + lastName + ", designation=" + designation
				+ ", dateOfBirth=" + dateOfBirth + "]";
	}
}

Employee bean definition in applicationContext.xml file is as below:

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
	<property name="propertyEditorRegistrars">
		<list>
			<bean class="com.howtodoinjava.demo.processors.CustomDateEditorRegistrar" />
		</list>
	</property>
</bean>

<!-- employeeDTO bean -->
<bean id="employeeDTO" class="com.howtodoinjava.demo.model.EmployeeDTO">
	<property name="firstName" value="Lokesh" />
	<property name="lastName" value="Gupta" />
	<property name="designation" value="Manager" />
	<property name="dateOfBirth" value="2007-09-30" />
</bean>

Let’s fetch the bean from context. It should have it’s dateOfBirth field populated with given date value.

public class TestSpringContext 
{
	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception 
	{
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

		EmployeeDTO employeeDTO = (EmployeeDTO) context.getBean("employeeDTO");
		
		System.out.println(employeeDTO.getDateOfBirth());
	}
}

Output:

Sun Sep 30 00:00:00 IST 2007

Great. Date value is set.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. vibhor

    July 6, 2020

    Basically It is used for converting one data type to other .for example if we assign string object to any custom class it will throw an exception .
    To avoid this I m using
    Spring Editor
    With annotation @InitBinder

  2. Deepika

    January 15, 2020

    Is CustomDateEditor Class can convert Object type of Date Values into String

  3. Binh Thanh Nguyen

    February 3, 2015

    Thanks, nice tip

Comments are closed on this article!

Search Tutorials

Spring Tutorial

  • Spring – Introduction
  • Spring – IoC Containers
  • Spring – IoC vs. DI
  • Spring – Bean Scopes
  • Spring – Bean Life Cycle
  • Spring – Bean Postprocessors
  • Spring – Autowiring
  • Spring – Annotations
  • Spring – Stereotype Annotations
  • Spring – Task Scheduling
  • Spring – Timer Task
  • Spring – Events
  • Spring – Message Source
  • Spring – ResourceLoader
  • Spring – Property Editor
  • Spring – Send Email
  • Spring – Version-less Schema
  • Spring – Interview Questions
  • Spring – Best Practices

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)