How to initialize Java Date object in Spring xml configuration file?

Consider this simple example -

public class Person { private String name; private Date dateOfBirth; // getters and setters here... } 

To initialize Person as a Spring bean, I can write the following.

 <bean id = "Michael" class = "com.sampleDomainName.Person"> <property name = "name" value = "Michael" /> </bean> 

But in the definition of bean above, how can I set dateOfBirth?

For example, I want to set dateOfBirth as

 1998-05-07 
+8
java spring xml configuration
source share
4 answers

One of the answers given here is useful, but he needs more information. You must provide constructor arguments for CustomDateEditor.

 <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 = "customDateEditor" class="org.springframework.beans.propertyeditors.CustomDateEditor"> <constructor-arg> <bean class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd" /> </bean> </constructor-arg> <constructor-arg value="true" /> </bean> 

Now we can do

 <property name="dateOfBirth" value="1998-05-07" /> 
+2
source share

Treat it like any other POJO (which is)

 <property name="dateOfBirth"> <bean class="java.util.Date" /> </property> 

If you need to use an explicit value (e.g. 1975-04-10), just call one of the other constructors (although those that take year-month-day are deprecated). You can also use the explicit java.beans.PropertyEditor , which Spring rolls already (see Section 6.4.2 ; that you can write your own editors and register them for your own types). You need to register CustomEditorConfigurer in your configuration:

 <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date" value="org.springframework.beans.propertyeditors.CustomDateEditor"/> </map> </property> </bean> 

Then your data looks like this:

 <property name="dateOfBirth" value="1975-04-10" /> 

I could add that Date not a suitable data type for storing a date of birth, because Date really instant. You can look at Joda and use the LocalDate class.

+7
source share

Use CustomDateEditor . He was in Spring from the first days.

0
source share

Spring enter date in bean property - CustomDateEditor

This article contains two suggestions:

  • Factory bean
  • CustomDateEditor

I suggest "Factory Bean" because CustomDateEditor is not supported in Spring 4.0+, and the Factory bean is simple enough to use.

 <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-2.5.xsd"> <bean id="dateFormat" class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd" /> </bean> <bean id="customer" class="com.mkyong.common.Customer"> <property name="date"> <bean factory-bean="dateFormat" factory-method="parse"> <constructor-arg value="2010-01-31" /> </bean> </property> </bean> </beans> 
-one
source share

All Articles