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

后端 未结 4 2069
生来不讨喜
生来不讨喜 2020-12-08 23:41

Consider this simple example -

public class Person
 {
    private String name;
    private Date dateOfBirth;

    // getters and setters here...
 }
<         


        
相关标签:
4条回答
  • 2020-12-09 00:21

    Spring inject Date into bean property – CustomDateEditor

    This paper give two suggestions:

    1. Factory bean
    2. CustomDateEditor

    I suggest the "Factory Bean" because the CustomDateEditor is not supported in Spring 4.0+ and the Factory Bean is easy 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>
    
    0 讨论(0)
  • 2020-12-09 00:26

    Use a CustomDateEditor. It's been in Spring since the early days.

    0 讨论(0)
  • 2020-12-09 00:30

    One of the answers mentioned here is useful, but it needs additional information. The constructor arguments for the CustomDateEditor need to be supplied.

    <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" />
    
    0 讨论(0)
  • 2020-12-09 00:34

    Treat it like any other POJO (which is is)

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

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

    <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:

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

    I might add that Date is not an appropriate data type to store a date-of-birth because Date is really an instant-in-time. You might like to look at Joda and use the LocalDate class.

    0 讨论(0)
提交回复
热议问题