Hi,
I have a spring boot project which relies on a custom converter class to convert between types. This class is annotated with @Component.
Previously this converter worked perfectly fine and all it did was map between the fields of one type to the fields of another type. However I've now got the problem where requires another spring component (a field has a non-linear mapping between types so we require this external service to get the correct mapping); we'll call this mappingService.
As a result it no longer works and I get a converter not found exception. I have tried constructor injection, and field injection and same bad result. I also tried setter injection and the converter was found but the mappingService was null.
I find this pretty peculiar as mappingService works as its injected elsewhere in the app but it can't be injected into the converter
Comment From: bclozel
Could you create a minimal, sample application using start.spring.io to illustrate this problem? A project we can git clone or download would be great. Thanks!
Comment From: officialpatterson
Heres a reproducible example https://github.com/officialpatterson/converterinjectionbug
I'm certain it has something to do with the converter depending on a component that also depends on ConversionService
Comment From: bclozel
There is a dependency cycle - this prevents your custom converter from being registered in a ConversionService.
Here are the details:
o.s.b.autoconfigure.web.servlet.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#addFormattersscans forConverterand other beans and configures them on theWebConversionServicebean that it's about to contributeSimpleComponentis injected with thatWebConversionServiceMyExternalConversionServiceis injected with thatWebConversionServiceMyOldClassToMyNewClassConverteris injected withMyExternalConversionService
Because of that cycle, the Converter is never picked up by the WebConversionService. You'll need to break this conceptual cycle in order to achieve what you're trying to do.