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

时间:2022-08-14 21:52:11

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定义中,如何设置日期?

For eg. I want to set the dateOfBirth as

如。我想把出生日期设为

1998-05-07

4 个解决方案

#1


7  

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

如果您需要使用显式值(比如1975-04-10),那么只需调用其他构造函数之一(尽管使用年-月-日的构造函数被弃用)。您还可以使用显式java.bean。已经使用了Spring卷的PropertyEditor(参见第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 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.

我可能要补充的是,日期不是存储出生日期的合适数据类型,因为日期实际上是一个实时的数据类型。您可能希望查看Joda并使用LocalDate类。

#2


2  

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

#3


0  

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

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

#4


-1  

Spring inject Date into bean property – CustomDateEditor

Spring注入日期到bean属性- CustomDateEditor。

This paper give two suggestions:

本文提出两点建议:

  1. Factory bean
  2. 工厂bean
  3. CustomDateEditor
  4. 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.

我建议使用“工厂Bean”,因为在Spring 4.0+中不支持CustomDateEditor,并且工厂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>

#1


7  

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

如果您需要使用显式值(比如1975-04-10),那么只需调用其他构造函数之一(尽管使用年-月-日的构造函数被弃用)。您还可以使用显式java.bean。已经使用了Spring卷的PropertyEditor(参见第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 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.

我可能要补充的是,日期不是存储出生日期的合适数据类型,因为日期实际上是一个实时的数据类型。您可能希望查看Joda并使用LocalDate类。

#2


2  

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

#3


0  

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

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

#4


-1  

Spring inject Date into bean property – CustomDateEditor

Spring注入日期到bean属性- CustomDateEditor。

This paper give two suggestions:

本文提出两点建议:

  1. Factory bean
  2. 工厂bean
  3. CustomDateEditor
  4. 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.

我建议使用“工厂Bean”,因为在Spring 4.0+中不支持CustomDateEditor,并且工厂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>