Spring教程 - Spring日期属性
2018-01-09 19:06 更新
Spring教程 - Spring日期属性
以下部分显示如何将数据填充到java.util.Date类型值。
Java Bean
以下部分显示如何将数据填充到java.util.Date类型值。...
package com.www.w3cschool.cnmon;
import java.util.Date;
//from w w w . j a v a2 s . c om
public class Customer {
Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "Customer [date=" + date + "]";
}
}
工厂bean
以下代码显示如何将String值解析为Date值,然后设置为Java bean。
以下代码显示如何将String值解析为Date值,然后设置为Java bean。...
在 constructor-arg 标记中,它将值设置为日期属性。
<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.www.w3cschool.cnmon.Customer">
<property name="date">
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2010-01-31" />
</bean>
</property>
</bean>
</beans>
Download Java2s_Spring_DateFactory.zip
CustomDateEditor
在第二种填充日期值的方法中,我们使用CustomDateEditor类。
在bean xml配置文件中,它声明了一个CustomDateEditor类将String转换为java.util.Date。
<bean id="dateEditor"
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>
然后它声明CustomEditorConfigurer使Spring转换类型为java.util.Date的bean属性。
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<ref local="dateEditor" />
</entry>
</map>
</property>
</bean>
这里是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="dateEditor"
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>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<ref local="dateEditor" />
</entry>
</map>
</property>
</bean>
<bean id="customer" class="com.www.w3cschool.cnmon.Customer">
<property name="date" value="2010-02-31" />
</bean>
</beans>
运行应用程序。
package com.www.w3cschool.cnmon;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"SpringBeans.xml");
Customer cust = (Customer) context.getBean("customer");
System.out.println(cust);
}
}
Download Java2s_Date_CustomDateEditor.zip
以上内容是否对您有帮助:

免费 AI IDE


更多建议: