Injecting Date values in Spring bean definitions using Factory Bean

In this article, I will show you how to set date value in spring bean definition properties using Factory Bean.

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

Add the following dependencies in pom.xml




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) { = 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=""
	<bean id="dateFormat" class="java.text.SimpleDateFormat">
		<constructor-arg value="dd-MM-yyyy" />

	<bean id="employee" class="com.kruders.bean.Employee">
	   <property name="name" value="Puneet" />
	   <property name="dob">
			<bean factory-bean="dateFormat" factory-method="parse">
				<constructor-arg value="16-04-1984" />

3. Run Program

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

package com.kruders.core;

import org.springframework.context.ApplicationContext;

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 46.1

Figure 46.1 Figure 46.1

You can download the source code of this example here.

No comments yet.

Leave a Reply