CustomEditerConfigurer 版本前后的一些变化 Spring
自安为Spring初学者,在看书学习Spring时,遇到了一CustomEditerConfigurer的讲解以及示例,结果在自己电脑运行时会抛出异常。
异常如下:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configBean' defined in file
[E:\My Progarm\Java Web\CustomEditerConfigurer\beans-config.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
[onlyfun.UserEditor] to required type [java.lang.Class] for property 'customEditors[onlyfun.User]':
PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [onlyfun.UserEditor] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:171) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520) at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140) at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84) at onlyfun.SpringDemo.main(SpringDemo.java:12) Caused by: 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 [onlyfun.UserEditor] to required type [java.lang.Class] for property 'customEditors[onlyfun.User]':
PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [onlyfun.UserEditor] at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:596) at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603) at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:204) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1527) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1486) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ... 11 more Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [onlyfun.UserEditor] to required type [java.lang.Class]
for property 'customEditors[onlyfun.User]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor]
returned inappropriate value of type [onlyfun.UserEditor] at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:298) at org.springframework.beans.TypeConverterDelegate.convertToTypedMap(TypeConverterDelegate.java:655) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:222) at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576) ... 17 more
没太看明白,于是在网上搜索了一下,也没找到解决方法,想到看看文档,自己使用的是Spring 4.2.4,而我看的书是1.*.* 版本的,十分久远。
最后发现是bean定义文件(是这么叫吗?)中定义CustomEditerConfigurer时版本前后发生了一点点变化。
下面是书中的示例:
1 <bean id="configBean" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> 2 <property name="customEditors"> 3 <map> 4 <!-- 意思是User Bean 中的属性会先在UserEditer中处理后在更新到实例中 --> 5 <entry key="onlyfun.User"> 6 <bean class="onlyfun.UserEditor"/> 7 </entry> 8 </map> 9 </property> 10 </bean>
这里是将自定义的编辑器在<entry></entry>中是以<bean class="自定义的编辑器类">标签的形式出现的。
而在文档中的示例为:
1 <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> 2 <property name="customEditors"> 3 <map> 4 <entry key="java.util.Date" value="mypackage.MyCustomDateEditor"/> 5 <entry key="mypackage.MyObject" value="mypackage.MyObjectEditor"/> 6 </map> 7 </property> 8 </bean>
可以发现这里是把 自定义的编辑器 放在了<entry/>的value属性中。即:
<entry key="java.util.Date" value="自定义的编辑器类"/>
发现了这点小小的变化后,把书中的代码修改了一下
<entry key="onlyfun.User" value="onlyfun.UserEditor"/>
结果运行成功。
至于旧版本使用<bean>标签,好像是为了在<bean>标签中加入<property>标签来定义属性。
然后再文档里有这么一小段:
Note, that you shouldn't register PropertyEditor bean instances via the customEditors property as PropertyEditors are stateful and the instances will then have to be synchronized for every editing attempt. In case you need control over the instantiation process of PropertyEditors, use a PropertyEditorRegistrar to register them.
抱歉英文不好,不过意思好像就是不可以这么写。
.End
第一次发文,写的不好请看客们多多包涵。

浙公网安备 33010602011771号