Spring 专题
专题目录
您的位置:java > Spring专题 > Spring注入日期到bean属性-CustomDateEditor
Spring注入日期到bean属性-CustomDateEditor
作者:--    发布时间:2019-11-20
这一个spring例子向您展示如何为bean属性注入一个“日期”。
package com.h3.common;

import java.util.date;

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配置文件
<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="customer" class="com.h3.common.customer">
		<property name="date" value="2015-12-31" />
	</bean>

</beans>

执行-运行程序输出

package com.h3.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);
		
	}
}
可能会遇到如下错误信息:
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

解决办法

在spring中,可以通过两种方式注入日期:

1. factory bean

声明一个dateformat bean,在“customer” bean,引用 “dateformat” bean作为一个工厂bean。该工厂方法将调用simpledateformat.parse()自动转换成字符串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.h3.common.customer">
		<property name="date">
			<bean factory-bean="dateformat" factory-method="parse">
				<constructor-arg value="2015-12-31" />
			</bean>
		</property>
	</bean>

</beans>

2. customdateeditor

声明一个 customdateeditor 类将字符串转换成 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转换 bean 属性,其类型为java.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>
bean配置文件的完整例子(applicationcontext.xml)。
<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.h3.common.customer">
		<property name="date" value="2015-12-31" />
	</bean>

</beans>

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册