簡體   English   中英

Spring無法注冊轉換器

[英]Spring Can't register converter

我在這里跟隨這個例子。 我的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

如果您正在使用<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>

這應確保僅存在1個conversionService(您的)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM