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)

この通話から戻ると、コンバーターが追加されていません。

何か案は?

ありがとう

-- llappall

4

1 に答える 1

0

Spring MVC の構成に使用<mvc:annotation-driven/>している場合、内部で 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>

これにより、conversionService(yours) が 1 つだけ存在するようになります。

于 2012-06-26T21:31:55.577 に答える