Spring无法注册转换器

时间:2012-06-26 20:31:28

标签: string spring hashmap converter

我正在关注示例here。 我的applicationContext具有以下内容:

<bean id="conversionService"
      class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean class="org.mypackage.MyFilterConverter"/>
        </set>
    </property>
</bean>

我的转换器看起来像这样:

public class MyFilterConverter implements Converter<String, HashMap<String, List<MyClass>>> { ...

我的问题:当我

@Autowired
private ConversionService conversionService;

并尝试使用它,conversionService只有默认值,而不是MyFilterConverter。

我跟着堆栈跟踪到

GenericConversionService.addConverter(GenericConverter converter)

当我从此电话回来时,我的转换器未添加。

有什么想法吗?

感谢

- llappall

1 个答案:

答案 0 :(得分:0)

如果您正在使用<mvc:annotation-driven/>来配置Spring MVC,那么它会在内部创建一个conversionService并且可能会覆盖您的conversionService,覆盖的方法是用此替换<mvc:annotation-driven/>(如果您使用的是Spring 3.1) ):

<bean name="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="conversionService" ref="conversionService"></property>
            <property name="validator">
                <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
            </property>
        </bean>
    </property>
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
        </list>
    </property>
</bean>

这应该确保只存在1个conversionService(你的)。

相关问题