How to configure Spring ConversionService with java config?

I have this 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> 

He configures the converters without any problems.

But then this code cannot do 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 part of the configuration is scanned by context - I checked it with the debugger. Where could the problem be?

+6
source share
3 answers

From my point of view, your problem is the name Bean . If you do not specify an explicit name using @Bean(name="conversionService") , the name to be used will be getConversionService .

From the documentation :

The name of this bean or plural, aliases for this bean. If you leave the unspecified name bean, this is the name of the annotated method. If specified, the method name is ignored.

+10
source

In SpringMVC, you can extend the WebMvcConfigurerAdapter and use it for a Java based configuration. To register custom converters, you can change the "addFormatters" -Method, like this

 @Configuration @EnableWebMvc @ComponentScan(basePackages = { "..." }) public class ApplicationConfiguration extends WebMvcConfigurerAdapter { @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Bean public InternalResourceViewResolver getInternalResourceViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } @Override public void addFormatters(FormatterRegistry formatterRegistry) { formatterRegistry.addConverter(getMyConverter()); } @Bean public StringToCounterConverter getMyConverter() { return new StringToCounterConverter(); } } 
+8
source

When you turn on logging, you'll see which Beans are created by Spring, as described here .

Log Configuration

  <logger name="org.springframework.beans" level="DEBUG" /> 

Log output

  DEBUG (AbstractAutowireCapableBeanFactory.java:458) - Finished creating instance of bean 'getConversionService' 

I copy + pasted your code, and it worked without changing the name. I introduced ConversionService as follows:

 @Resource private ConversionService conversionService; 

This works because of Autowiring by type . Perhaps you had two ConversionService beans.

0
source

Source: https://habr.com/ru/post/919386/


All Articles