如何在 Spring xml 配置文件中初始化 Java Date 对象? [英] How to initialize a Java Date object in Spring xml configuration file?
本文介绍了如何在 Spring xml 配置文件中初始化 Java Date 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑这个简单的例子 -
Consider this simple example -
public class Person
{
private String name;
private Date dateOfBirth;
// getters and setters here...
}
为了将Person初始化为Spring bean,我可以这样写.
In order to initialize Person as a Spring bean, I can write the following.
<bean id = "Michael" class = "com.sampleDomainName.Person">
<property name = "name" value = "Michael" />
</bean>
但是在上面的bean定义中,如何设置dateOfBirth?
But in the above bean definition, how can I set the dateOfBirth?
例如.我想将 dateOfBirth 设置为
For eg. I want to set the dateOfBirth as
1998-05-07
推荐答案
此处提到的答案之一很有用,但需要其他信息.需要提供 CustomDateEditor 的构造函数参数.
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>
现在可以了
<property name="dateOfBirth" value="1998-05-07" />
这篇关于如何在 Spring xml 配置文件中初始化 Java Date 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文