简体   繁体   English

Spring无法注册转换器

[英]Spring Can't register converter

I'm following the example here . 我在这里跟随这个例子。 My applicationContext has the following: 我的applicationContext具有以下内容:

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

My converter looks like this: 我的转换器看起来像这样:

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

My problem: when I 我的问题:当我

@Autowired
private ConversionService conversionService;

and try to use it, the conversionService has only the default ones, not MyFilterConverter. 并尝试使用它,conversionService仅具有默认值,没有MyFilterConverter。

I followed the stack trace down to 我遵循堆栈跟踪到

GenericConversionService.addConverter(GenericConverter converter)

When I come back from this call, my converter is not added. 当我从此呼叫回来时,未添加我的转换器。

Any ideas? 有任何想法吗?

thanks 谢谢

-- llappall -llappall

If you are using <mvc:annotation-driven/> to configure Spring MVC then it creates a conversionService internally and may be overriding your conversionService, the way to override is to replace <mvc:annotation-driven/> with this(if you are using Spring 3.1): 如果您正在使用<mvc:annotation-driven/>配置Spring MVC,则它将在内部创建一个conversionService,并且可能覆盖您的conversionService,覆盖的方法是将this替换<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>

This should ensure that only 1 conversionService(yours) will be present. 这应确保仅存在1个conversionService(您的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM