Friday, 10 August 2018

How to inject Date into bean property using Factory bean in Spring

Click here to watch on Youtube:

Click the below Image to Enlarge:

How to inject Date into bean property using Factory bean in Spring

How to inject Date into bean property using Factory bean in Spring

<project xmlns="" xmlns:xsi=""



        <!-- Spring 5 dependencies -->


package com.ram.model;

import java.util.Date;

public class Employee
    private String name;
    private Date dateOfBirth;

    public String getName()
        return name;

    public void setName(String name)
    { = name;

    public Date getDateOfBirth()
        return dateOfBirth;

    public void setDateOfBirth(Date dateOfBirth)
        this.dateOfBirth = dateOfBirth;

    public String toString()
        return "Employee [name=" + name + ", dateOfBirth=" + dateOfBirth + "]";



<beans xmlns=""

    <bean id="dateFormat" class="java.text.SimpleDateFormat">
        <constructor-arg value="yyyy-MM-dd" />

    <bean id="employee" class="com.ram.model.Employee">

        <property name="name">

        <property name="dateOfBirth">
            <bean factory-bean="dateFormat" factory-method="parse">
                <constructor-arg value="2010-01-31" />



package com.ram.core;

import org.springframework.context.ApplicationContext;

import com.ram.model.Employee;

public class App
    public static void main(String[] args)
        ApplicationContext context = new ClassPathXmlApplicationContext(


        Employee employee = (Employee) context.getBean("employee");
                "Got employee object from the ApplicationContext(Spring Container)");





Jul 31, 2018 9:55:07 AM prepareRefresh
INFO: Refreshing startup date [Tue Jul 31 09:55:07 IST 2018]; root of context hierarchy
Jul 31, 2018 9:55:07 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Got employee object from the ApplicationContext(Spring Container)
Employee [name=Peter, dateOfBirth=Sun Jan 31 00:00:00 IST 2010]

Click the below link to download the code:

Github Link:

Bitbucket Link:

See also:

  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment