0

我在这里按照示例进行操作。我的 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)

当我从这个电话回来时,我的转换器没有被添加。

有任何想法吗?

谢谢

——拉帕

4

1 回答 1

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(yours) 存在。

于 2012-06-26T21:31:55.577 回答