CustomDateEditor not working in Spring 4.1.6 -
i using date dob; field in pojo class in spring 4.1.6 using maven below files associated app.
// pojo class.
package com.aamir; import java.util.date; public class student { private string firstname; private date dob; public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public date getdob() { return dob; } public void setdob(date dob) { this.dob = dob; } }
// client class
package com.aamir; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class clientstud1 { public static void main(string[] args) { applicationcontext context = new classpathxmlapplicationcontext("beans.xml"); student student = (student) context.getbean("s1"); system.out.println(student.getfirstname()); system.out.println(student.getdob()); }
} // , beans.xml file
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="s1" class="com.aamir.student"> <property name="firstname" value="aamir"/> <property name="dob" value="12-12-1996"/> </bean> <bean id="dateeditor" class="org.springframework.beans.propertyeditors.customdateeditor"> <constructor-arg> <bean class="java.text.simpledateformat"> <constructor-arg value="dd-mm-yyyy"/> </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" value-ref="dateeditor"/> </map> </property> </bean> </beans>
and exception every time try run client
org.springframework.beans.factory.beancreationexception: error creating bean name 'org.springframework.beans.factory.config.customeditorconfigurer#0' defined in class path resource [beans.xml]: initialization of bean failed; nested exception org.springframework.beans.typemismatchexception: failed convert property value of type 'java.util.linkedhashmap' required type 'java.util.map' property 'customeditors'; nested exception java.lang.illegalargumentexception: cannot convert value of type [org.springframework.beans.propertyeditors.customdateeditor] required type [java.lang.class] property 'customeditors[java.util.date]': propertyeditor [org.springframework.beans.propertyeditors.classeditor] returned inappropriate value of type [org.springframework.beans.propertyeditors.customdateeditor]
note propertyeditor
stateful, should not register instances directly.
- register property editor classes via
customeditor
property - add
propertyeditorregistrars
viapropertyeditorregistrars
property incustomeditorconfigurer
as want configure property editor instance, use property editor registrar instead.
<bean id="customeditorconfigurer" class="org.springframework.beans.factory.config.customeditorconfigurer"> <property name="propertyeditorregistrars"> <list> <bean class="org.example.customdateeditorregistrar"/> </list> </property> </bean>
Comments
Post a Comment