地址: http://k2java.blogspot.com/2011/04/spring-how-to-pass-date-into-bean.html
Thursday, April 14, 2011
Spring – How to pass a Date into bean property (CustomDateEditor )
Simple method may not work
Generally, Spring developer are not allow to pass a date format parameter into bean property via DI.
For example,
publicclass CustomerService
{
Date date;
publicDate getDate(){
return date;
}
publicvoid setDate(Date date){
this.date = date;
}
}
Bean configuration file
<beansxmlns="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">
<beanid="customerService"class="com.services.CustomerService">
<propertyname="date"value="2010-01-31"/>
</bean>
</beans>
Run it
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.customer.services.CustomerService;
publicclass App
{
publicstaticvoid main(String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext(newString[]{"Spring-Customer.xml"});
CustomerService cust =(CustomerService)context.getBean("customerService");
System.out.println(cust.getDate());
}
}
Error message prompt.
Caused by: org.springframework.beans.TypeMismatchException:
Failed to convert property value of type [java.lang.String] to
required type [java.util.Date] for property 'date';
nested exception is java.lang.IllegalArgumentException:
Cannot convert value of type [java.lang.String] to
required type [java.util.Date] for property 'date':
no matching editors or conversion strategy found
Solution
There are two solutions available.
1. Factory bean
Declare a dateFormat bean, and reference it as a factory bean from the date property. The factory method will call the SimpleDateFormat.parse() menthod to convert the String into Date object automatically.
<beansxmlns="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">
<beanid="dateFormat"class="java.text.SimpleDateFormat">
<constructor-argvalue="yyyy-MM-dd"/>
</bean>
<beanid="customerService"class="com.mkyong.customer.services.CustomerService">
<propertyname="date">
<beanfactory-bean="dateFormat"factory-method="parse">
<constructor-argvalue="2010-01-31"/>
</bean>
</property>
</bean>
</beans>
2. Property editors (CustomEditorConfigurer + CustomDateEditor)
Declare a CustomDateEditor class to convert the String into java.util.Date properties.
<beanid="dateEditor"
class="org.springframework.beans.propertyeditors.CustomDateEditor">
<constructor-arg>
<beanclass="java.text.SimpleDateFormat">
<constructor-argvalue="yyyy-MM-dd"/>
</bean>
</constructor-arg>
<constructor-argvalue="true"/>
</bean>
Register the CustomDateEditor in CustomEditorConfigurer, so that the Spring will convert the properties whose type is java.util.Date.
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
<beanclass="org.springframework.beans.factory.config.CustomEditorConfigurer">
<propertyname="customEditors">
<map>
<entrykey="java.util.Date">
<reflocal="dateEditor"/>
</entry>
</map>
</property>
</bean>
Bean configuration file.
<beansxmlns="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">
<beanid="dateEditor"
class="org.springframework.beans.propertyeditors.CustomDateEditor">
<constructor-arg>
<beanclass="java.text.SimpleDateFormat">
<constructor-argvalue="yyyy-MM-dd"/>
</bean>
</constructor-arg>
<constructor-argvalue="true"/>
</bean>
<beanclass="org.springframework.beans.factory.config.CustomEditorConfigurer">
<propertyname="customEditors">
<map>
<entrykey="java.util.Date">
<reflocal="dateEditor"/>
</entry>
</map>
</property>
</bean>
<beanid="customerService"class="com.mkyong.customer.services.CustomerService">
<propertyname="date"value="2010-02-31"/>
</bean>
</beans>