Spring inject Date into bean property – CustomDateEditor
来源:互联网 发布:淘宝网魔豆妈妈店铺 编辑:程序博客网 时间:2024/06/06 09:08
转载自:http://www.mkyong.com/spring/spring-how-to-pass-a-date-into-bean-property-customdateeditor/
Spring example to show you how to inject a “Date” into bean property.
package com.mkyong.common; import java.util.Date; public class Customer { Date date; public Date getDate() {return date;} public void setDate(Date date) {this.date = date;} @Overridepublic String toString() {return "Customer [date=" + date + "]";} }
Bean configuration file
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customer" class="com.mkyong.common.Customer"><property name="date" value="2010-01-31" /></bean> </beans>
Run it
package com.mkyong.common; 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); }}
You will encounter follow error messages :
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] torequired type [java.util.Date] for property 'date': no matching editors or conversion strategy found
Solution
In Spring, you can inject a Date via two methods :
1. Factory bean
Declare a dateFormat bean, in “customer” bean, reference “dateFormat” bean as a factory bean. The factory method will callSimpleDateFormat.parse()
to convert String into Date object automatically.
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://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>
2. CustomDateEditor
Declares a CustomDateEditor class to convert String into 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>
And declares another “CustomEditorConfigurer”, to make Spring convert bean properties whose type isjava.util.Date.
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"><property name="customEditors"><map><entry key="java.util.Date"><ref local="dateEditor" /></entry></map></property></bean>
Full example of bean configuration file.
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://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.mkyong.common.Customer"><property name="date" value="2010-02-31" /></bean> </beans>
Download Source Code
- Spring inject Date into bean property – CustomDateEditor
- Spring inject Date into bean property – CustomDateEditor
- Spring inject Date into bean property
- How to inject value into bean properties in Spring
- Spring注入日期到bean属性-CustomDateEditor
- spring通过3种写法来inject value到bean的property中。
- spring通过3种写法来inject value到bean的property中
- spring bean property ref
- (9)Tag:property、set、push、bean、if、iterate、date
- Inject $scope into factory service ??
- Spring From the Trenches: Injecting Property Values Into Configuration Beans
- spring inject static field
- Spring @Autowired与@Inject
- Spring @Named @Inject
- spring .xml中bean下的property属性
- 通过Spring配置文件中bean中的property赋值
- 赋值-通过Spring配置文件中bean中的property
- 赋值-通过Spring配置文件中bean中的property
- PB 并发控制
- POJ 3084 最大流最小割求割边
- hibernate 初学
- 常见Android笔试题
- Virtual Box 虚拟机和主机之间进行通信的设置方法
- Spring inject Date into bean property – CustomDateEditor
- Android上传文件至服务器
- DDK(Driver Developer Kit)和WDK(Windows Driver Kit)的区别
- c库函数参考手册
- POJ 3204 求使得最大流增大的边
- 把一个逗号分开的字符串转化为数组的两个方法
- 实用的20条编程经验
- layoutSubviews 用法
- android Button事件处理