Convert strings to date in Spring 4.0

I educate myself on Spring 4.0.0 M3 The code below is

Bean

package org.chebus.springs; import java.util.Date; public class Traingle { private String name; private int height; private Date date; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public void drawShape() { System.out.println(getName() + " Traingle height " + getHeight() + " Date = " + getDate()); } } 

home

 ApplicationContext ctx = new ClassPathXmlApplicationContext("org/chebus/springs/Spring.xml"); Traingle traingle = ctx.getBean("traingle",Traingle.class); traingle.drawShape(); 

XML configuration

 <?xml version="1.0" encoding="UTF-8"?> <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 = "traingle" class="org.chebus.springs.Traingle"> <property name="name" value = "RightAngled"/> <property name="height" value = "20"/> <property name="date" value = "2013-09-10"/> </bean> <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> 

An exception:

java.lang.IllegalArgumentException: cannot convert value of type [org.springframework.beans.propertyeditors.CustomDateEditor] to the required type [java.lang.Class] for the property 'customEditors [java.util.Date]': PropertyEditor [org.springframework. beans.propertyeditors.ClassEditor] returned an inconsistent value of type [org.springframework.beans.propertyeditors.CustomDateEditor] in org.springframework.beans.TypeConverterDelegate.convertIfNecessary (TypeConverterDelegate.java:260fteTemeterTeconTemeterTevertTemeterTemeterTepeebebe.ste .java: 620) in org.springframework.beans.TypeConverterDelegate.convertIfNecessary (TypeConverterDelegate.java:205) in org.springframework.beans.BeanWrapperImpl.convertIfNecessary (BeanWrapperImpl.java:459) ... more

I don’t know where I am mistaken. I appreciate your help. Thanks!

+6
source share
2 answers

Good catch, this seems to be a new behavior with Spring 4.0+, your code works cleanly with Spring 3.2.x version.

The reason is because the customEditors type in CustomEditorConfigurer has changed using Spring 4.0+. If it is of type Map<String, ?> With Spring 3.2.x, then it is Map<Class<?>, Class<? extends PropertyEditor>> Map<Class<?>, Class<? extends PropertyEditor>> since Spring 4.0+.

The fix is ​​to create your own PropertyEditorRegistrar instead, this way:

 import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.PropertyEditorRegistry; import org.springframework.beans.propertyeditors.CustomDateEditor; public class CustomDateEditorRegistrar implements PropertyEditorRegistrar { @Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false)); } } 

and use this in the configuration:

 <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="propertyEditorRegistrars"> <list> <bean class="dateeditor.CustomDateEditorRegistrar"/> </list> </property> </bean> 
+20
source

By default, spring has no built-in date values. Well here: How to initialize a Java Date object in a Spring xml configuration file?

0
source

Source: https://habr.com/ru/post/953520/


All Articles