文章地址:http://blog.csdn.net/hengyunabc/article/details/14107963
在Spring MVC里可以通过message converter机制来对数据进行格式化,但是在普通的Spring xml配置里就无能为力了。
在网上找了一通,发现也有很多人苦恼这个问题,不妨记录下。
Spring 自带一个PropertyEditor的机制,但是这个是全局的,假如你的配置文件要是和别人有不同的话,那就悲剧了,比如你配置的时间的格式是“yyyy-mm-dd",别人的是"yyyy-mm-dd-hh”。别外自定义的PropertyEditor还要注册到ApplicationContext里,非常麻烦,而且这个也是一个全局的配置。
http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/validation.html#beans-beans-conversion
别外Spring文档里提到了一个BeanInfo机制,但实际这个机制很坑爹,这个对Date无用,因为CustomDateEditor默认是没有注册到Spring里的。
Customizable property editor for java.util.Date, supporting a custom DateFormat. NOT registered by default. Must be user registered as needed with appropriate format.
下面来看下如何利用Spring提供的Factory bean机制来初始化Date对象的:
<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>
来自:http://www.mkyong.com/spring/spring-how-to-pass-a-date-into-bean-property-customdateeditor/
至于Calendar,则没有Date那么容易了,因为Calendar是一个抽象类。一般都是通过Calendar.getInstance()来得到一个实例的。
Spring forum上有个网友给出了一个很巧妙的办法:
http://forum.spring.io/forum/spring-projects/container/22111-inject-java-util-calendar-instance
package org.spring.forum; import java.util.Calendar; import org.springframework.beans.factory.config.AbstractFactoryBean; public class CalendarFactoryBean extends AbstractFactoryBean { /** * Year; defaults to zero. */ private int year = 0; /** * Month; defaults to zero. */ private int month = 0; /** * Date; defaults to zero. */ private int date = 0; /** * Number of hour of day (24 hour); defaults to zero. */ private int hourOfDay = 0; /** * Number of minutes; defaults to zero. */ private int minute = 0; /** * Number of seconds; defaults to zero. */ private int second = 0; public void setYear(int year) { this.year = year; } public void setMonth(int month) { this.month = month; } public void setDate(int date) { this.date = date; } public void setHourOfDay(int hourOfDay) { this.hourOfDay = hourOfDay; } public void setMinute(int minute) { this.minute = minute; } public void setSecond(int second) { this.second = second; } @Override protected Object createInstance() throws Exception { Calendar calendar = Calendar.getInstance(); calendar.set(this.year, this.month, this.date, this.hourOfDay, this.minute, this.second); return calendar; } public Class getObjectType() { return Calendar.class; } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="janFirst2006" class="org.spring.forum.CalendarFactoryBean"> <property name="year" value="2006" /> <property name="month" value="0" /> <property name="date" value="1" /> </bean> <bean id="janFirst2006_noon" class="org.spring.forum.CalendarFactoryBean"> <property name="year" value="2006" /> <property name="month" value="0" /> <property name="date" value="1" /> <property name="hourOfDay" value="12" /> </bean> </beans>
如果非要用字符串的方式来配置一个Calendar,如"1999-09-09"。有两个思路,一个是把一个Date对象setter到一个Calendar里,这个就很绕弯了。
<bean id="calendarBean" class="java.util.Calendar" factory-method="getInstance"> <property name="time" ref="dateBean"/> </bean>
不过,扯了这么久,上面都是如何得到一个Date或者Calendar对象,如果硬是要是配置文件里直接用字符串的方式把Date或者Calendar注入到一个Bean,那么只用在Spring Context里注册一个全局的PropertyEditor了。