如何在 Spring xml 配置文件中初始化 Java Date 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11198297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:11:48  来源:igfitidea点击:

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

javaxmlspringconfiguration

提问by CodeBlue

Consider this simple example -

考虑这个简单的例子——

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

    // getters and setters here...
 }

In order to initialize Person as a Spring bean, I can write the following.

为了将 Person 初始化为 Spring bean,我可以这样写。

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

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

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

For eg. I want to set the dateOfBirth as

例如。我想将 dateOfBirth 设置为

1998-05-07

采纳答案by CodeBlue

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

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

回答by oxbow_lakes

Treat it like any other POJO (which is is)

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

<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.PropertyEditorwhich 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 CustomEditorConfigurerin your config:

如果您需要使用显式值(例如 1975-04-10),则只需调用其他构造函数之一(尽管不推荐使用年-月-日的构造函数)。您还可以使用显式java.beans.PropertyEditorwhich Spring rolls 已经(参见第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>

Then your data looks like:

然后你的数据看起来像:

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

I might add that Dateis not an appropriate data typeto store a date-of-birth because Dateis really an instant-in-time. You might like to look at Jodaand use the LocalDateclass.

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

回答by nkduqi

Spring inject Date into bean property – CustomDateEditor

Spring 将日期注入 bean 属性 - CustomDateEditor

This paper give two suggestions:

本文给出两点建议:

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

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

我建议使用“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>

回答by duffymo

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

使用CustomDateEditor。从一开始就是春天。