Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customEditorConfigurer' defined in class path resource [propertyEditorSupport.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.util.LinkedHashMap' to required type 'java.util.Map' for property 'customEditors'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [com.bean.study.bean.customerProperty] to required type [java.lang.Class] for property 'customEditors[java.util.Date]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [com.bean.study.bean.customerProperty]
问题概述:当我们注意一个日期类型的属性时,就需要配置属性编辑器,就需要继承PropertyEditorSupport ,重写setAsText方法,然后正确配置xml文件。
(这个问题困扰了我一天,一直到晚上2点才搞定。)
但是当我们一切配置好后,发现根本无效,既出现了上面的错误。
大家看看我的xml配置
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date"> <bean class="com.bean.study.bean.customerProperty"> <property name="format" > <value>yyyy-mm-dd</value> </property> </bean> </entry> </map> </property> </bean>打这个原因是因为我们的Spring的版本不一样造成的,通过查询源码发现,这个customEditors参数是接收的两个String类型的值 所以问题就出在这里,我们应该对map entry key进行更改。 更改后的代码效果如下:
<map>
<entry key="java.util.Date" value="com.bean.study.bean.customerProperty" />
</map>
key表示类型,value表示只对当前bean有效
如果这个bean需要注入值,我们可以在下面另外写一个bean进行注入。爆款云服务器s6 2核4G 低至0.46/天,具体规则查看活动详情