Using java.util.Date values in Spring bean definitions

In a Spring bean definition you can set value properties to a bean by using the value attribute in the property element. For example below I set the value of the firstName property of a bean with id spiros of type com.tzavelas.beans.Person to Spiros.

<bean id="spiros" class="com.tzavellas.beans.Person">
  <property name="firstName" value="Spiros"/>
</bean>

In Spring XML, by default, you can only set string and number values in bean properties. Now, what happens when the Javabean, you want to configure, has a value property that is of type java.util.Date?

Spring provides an extensible way to set any arbitrary object value to a bean property. This is done with a Javabean standard mechanism called PropertyEditors. PropertyEditors are objects whose purpose is to transform an object’s value to a string and vice versa. These objects implement the java.beans.PropertyEditor interface.

Spring has out of the box implemented some very useful PropertyEditors for common classes, like java.util.Date, java.io.File and others, that can be found in org.springframework.beans.propertyeditors package. To use the above editors in your spring configuration you have to first register them with a CustomEditorConfigurer. CustomEditorConfigurer is a BeanFactoryPostProcessor and runs after the BeanFactory is initialized.

In the below code I have an example where I register a CustomDateEditor that binds java.util.Date values to bean properties. Then I have a bean definition where I set a bean property to a date value.

The Java code for the example Javabean:

package com.tzavellas.beans;

import java.util.Date;

public class Person {

  private Date birthDate;
  private String firstName;

  public Date getBirthDate() {
    return birthDate;
  }
  public void setBirthDate(Date birthDate) {
    this.birthDate = birthDate;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
}

The XML configuration:

<bean id="customEditorConfigurer"
      class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  <property name="customEditors">
    <map>
      <entry key="java.util.Date">
        <bean class="org.springframework.beans.propertyeditors.CustomDateEditor">
          <constructor-arg index="0">
            <bean class="java.text.SimpleDateFormat">
              <constructor-arg value="dd/MM/yyyy"/>
            </bean>
          </constructor-arg>
          <constructor-arg index="1" value="false"/>
        </bean>
      </entry>
    </map>
  </property>
</bean>

<bean id="spiros" class="com.tzavellas.beans.Person">
  <property name="firstName" value="Spiros"/>
  <property name="birthDate" value="15/04/1980"/>
</bean>

As you can see I am passing a java.text.SimpleDateFormat to the constructor of the CustomDateEditor and later in the definition of the bean with id spiros I am setting a date property using the format I specified in the CustomDateEditor.

UPDATE: For the above code to work you have to use an ApplicationContext. A BeanFactory (like XmlBeanFactory) does not work here because BeanFactories do not auto-detect BeanFactoryPostProcessors like the CustomEditorConfigurer used in the above code.

4 thoughts on “Using java.util.Date values in Spring bean definitions

  1. This does work, but you have to use an ApplicationContext instead of a BeanFactory. BeanFactories do not auto-detect BeanFactoryPostProcessors like the CustomEditorConfigurer.

    Sorry I forgot to mention this in the article.

Comments are closed.