【问题标题】:How to initialize a Java Date object in Spring xml configuration file?如何在 Spring xml 配置文件中初始化 Java Date 对象?
【发布时间】:2012-06-25 22:30:03
【问题描述】:

考虑这个简单的例子 -

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

    // getters and setters here...
 }

为了将 Person 初始化为 Spring bean,我可以编写以下代码。

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

但是在上面的bean定义中,如何设置dateOfBirth呢?

例如。我想将 dateOfBirth 设置为

1998-05-07

【问题讨论】:

    标签: java xml spring configuration


    【解决方案1】:

    像对待任何其他 POJO(即是)一样对待它

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

    如果您需要使用显式值(例如 1975-04-10),则只需调用其他构造函数之一(尽管不推荐使用年月日的构造函数)。你也可以使用显式的java.beans.PropertyEditor which Spring rolls with already(参见6.4.2部分;注意你可以编写自己的编辑器并为你自己的类型注册它们)。您需要在配置中注册 CustomEditorConfigurer

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

    那么你的数据看起来像:

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

    我可能会补充一点,Date 不是存储出生日期的合适数据类型,因为Date 确实是一个即时 .您可能想查看 Joda 并使用 LocalDate 类。

    【讨论】:

    • 他可能想将其初始化为特定日期而不是当前时间。
    【解决方案2】:

    这里提到的一个答案很有用,但它需要额外的信息。需要提供 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>
    

    现在我们可以做

    <property name="dateOfBirth" value="1998-05-07" />
    

    【讨论】:

      【解决方案3】:

      Spring inject Date into bean property – CustomDateEditor

      本文给出两个建议:

      1. 工厂豆
      2. 自定义日期编辑器

      我建议使用“Factory Bean”,因为 Spring 4.0+ 不支持 CustomDateEditor,而且 Factory Bean 很容易使用。

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

      【讨论】:

      • 您能否提供链接所涵盖内容的一些基础知识?否则,如果将来无法访问此链接,则此答案将不再传达任何信息。
      • @DanLowe 嗨,我已经从我建议的链接中添加了一个示例
      【解决方案4】:

      使用CustomDateEditor。从早期开始就在春天。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-20
        • 2012-11-03
        • 1970-01-01
        相关资源
        最近更新 更多