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 + "]";
}
}
<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
<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>
<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>
<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>