Injecting Date values in Spring bean definitions using CustomDateEditor

In this article, I will show you how to set date value in spring bean definition properties using PropertyEditors. PropertyEditors are objects whose purpose is to transform an object’s value to a string and vice versa. To use the above editors in your spring configuration you have to first register them with a CustomEditorConfigurer.

	<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
				<entry key="java.util.Date">
					<ref local="customDateEditor" />
				</entry>
			</map>
		</property>
	</bean>

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.6</version>
    </dependency>

</dependencies>

1. POJO

Create Employee Class as following

package com.kruders.bean;

import java.util.Date;

public class Employee {
	private String name;
	private Date dob;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getDob() {
		return dob;
	}
	public void setDob(Date dob) {
		this.dob = dob;
	}
}

2. Configuration

Create Spring-Bean.xml and write the following code

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="customDateEditor" class="org.springframework.beans.propertyeditors.CustomDateEditor">
		<constructor-arg>
			<bean class="java.text.SimpleDateFormat">
				<constructor-arg value="dd-MM-yyyy" />
			</bean>
		</constructor-arg>
		<constructor-arg value="true" />
	</bean>
	
	<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
				<entry key="java.util.Date">
					<ref local="customDateEditor" />
				</entry>
			</map>
		</property>
	</bean>

	<bean id="employee" class="com.kruders.bean.Employee">
	   <property name="name" value="Puneet" />
	   <property name="dob" value="16-04-1984" />
   </bean>
</beans>

Create Main.java class that displays the employee details and run it as Java Application

3. Run Program

Create Main.java class that displays the employee details and run it as Java Application

package com.kruders.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kruders.bean.Employee;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Beans.xml");
		
		Employee employee = (Employee) context.getBean("employee");

		System.out.println("Name " + employee.getName());
		System.out.println("Date of Birth " + employee.getDob());
	}
}

When you run the above example you’ll get an output like:

Name Puneet
Date of Birth Mon Apr 16 00:00:00 IST 1984

The folder structure of the example is shown below in Figure 47.1

Figure 47.1 Figure 47.1

You can download the source code of this example here.


One Response to Injecting Date values in Spring bean definitions using CustomDateEditor

  1. java2novice June 30, 2014 at 4:41 pm #

    very nice…

Leave a Reply