如何使用java配置配置Spring ConversionService? [英] How to configure Spring ConversionService with java config?
问题描述
我有这样的xml:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="converters.AddressToStringConverter" />
<bean class="converters.StringToAddressConverter" />
</list>
</property>
</bean>
它配置转换器没有问题。
It configures converters without problems.
但是此代码不能使同样的:
But then this code fails to make the same:
@Configuration
public class ConversionConfiguration {
@Bean
public ConversionService getConversionService() {
ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
bean.setConverters(getConverters());
bean.afterPropertiesSet();
ConversionService object = bean.getObject();
return object;
}
private Set<Converter> getConverters() {
Set<Converter> converters = new HashSet<Converter>();
converters.add(new AddressToStringConverter());
converters.add(new StringToAddressConverter());
return converters;
}
}
它与调试器。在哪里可能是问题?
This piece of configuration gets scanned by context - I checked it with debugger. Where could be the problem?
推荐答案
从我的角度来看,你的问题是 Bean
名称。一旦不使用 @Bean(name =conversionService)
显式设置名称,将使用的名称为 getConversionService
。
From my point of view your problem is the Bean
name. Once you don't explicit set the name using @Bean(name="conversionService")
the name that will be used is getConversionService
.
从文档:
此Bean的名称,如果为复数,这个bean的别名。如果留下
unspecified,那么bean的名称就是注释方法的名称。
如果指定,方法名称将被忽略。
The name of this bean, or if plural, aliases for this bean. If left unspecified the name of the bean is the name of the annotated method. If specified, the method name is ignored.
这篇关于如何使用java配置配置Spring ConversionService?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!