错误为org.springframework.beans.propertyeditors.StringTrimmerEditor实例化bean

时间:2014-04-23 18:43:57

标签: java spring

这是我的applicationContext.xml的样子:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:sec="http://www.springframework.org/schema/security"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">

..... .....

             

<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
   <property name="customEditors">
        <map>
           <entry key="java.lang.String">
                <ref bean="stringTrimmerEditor"/>
            </entry>
        </map>
    </property>
 </bean>

我收到此错误:

  

org.springframework.beans.factory.BeanCreationException:错误   创建名为'customEditorConfigurer'的bean   ServletContext资源[/WEB-INF/spring-servlet.xml]:初始化   豆失败;嵌套异常是   org.springframework.beans.TypeMismatchException:无法转换   类型'java.util.LinkedHashMap'的属性值为必需的类型   属性'customEditors'的'java.util.Map';嵌套异常是   java.lang.IllegalArgumentException:无法转换类型的值   [org.springframework.beans.propertyeditors.StringTrimmerEditor]来   属性所需的类型[java.lang.Class]   'customEditors [java.lang.String]':PropertyEditor   [org.springframework.beans.propertyeditors.ClassEditor]返回   不合适的类型值   [org.springframework.beans.propertyeditors.StringTrimmerEditor]

1 个答案:

答案 0 :(得分:0)

documentation for the setCustomEditors method of the CustomEditorConfigurer class声明如下(强调我的):

  

指定要通过Map注册的自定义编辑器,使用所需类型的类名作为键,并将关联的PropertyEditor的类名称作为值

所以你传递的是类名,而不是bean本身,作为地图中每个条目的值。

尝试替换

<entry key="java.lang.String">
    <ref bean="stringTrimmerEditor"/>
</entry>

<击> 以

<击>
<entry key="java.lang.String"
       value="org.springframework.beans.propertyeditors.StringTrimmerEditor" />

<击>

<entry key="java.lang.String">
    <bean class="org.springframework.beans.propertyeditors.StringTrimmerEditor">
        <!-- change the value as necessary.  --> 
        <constructor-arg name="emptyAsNull" value="false" />  
    </bean>
</entry>