Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple conversionServices in spring-boot

Tags:

spring-boot

I have a boot application and in one of my facades I try to Autowire the conversionService like this:

@Autowired
private ConversionService conversionService;

as a result I get this:

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.core.convert.ConversionService] is defined: expected single matching bean but found 3: mvcConversionService,defaultConversionService,integrationConversionService
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1061)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 16 more

To overcome this I have added a Qualifier lilke this:

@Autowired
@Qualifier("mvcConversionService")
private ConversionService c;

and this all works. However all my custom converters are automatically added the to the mvcConversionService. And now I want to extend the ConversionService and add another method to it, however my converters are again added the to the mvcConversionService. Is there a way to tell spring-boot which conversionService to use to automatically register my converters there? I don't want to manually list all the converters to the new conversionService.

like image 382
Petar Tahchiev Avatar asked Jan 19 '26 04:01

Petar Tahchiev


1 Answers

Explanation

Spring will attempt to auto-create a ConversionService (in your case mvcConversionService) for the application. It has no qualifiers for what Converters will be registered so if you create a component/bean that implements one of the following types, it will be autoregistered to the service.

  1. Converter
  2. GenericConverter
  3. Printer
  4. Parser

Solution #1

Ignore it. If there is no harm done by registering these converters to the mvcConversionService, then it might not be worth your time to work around this restriction. Your converter classes will likely go unused.

Solution #2

Reproduce your own interfaces. You can copy the interface for ConversionService and Converter and name them how you like. Sure, the functionality is intended to be the same but this will prevent your Converter classes from being injected into any other ConversionService instances.

like image 80
hsoj48 Avatar answered Jan 21 '26 23:01

hsoj48



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!