Closed
Description
Liu, Yinwei David opened SPR-6415 and commented
Hi,
Currently, we can only register a CustomConverter by configuring it in Spring configuration file in Spring 3.0.0.RC2. However, we want the ability which allows all Converters to register themselves into the global ConversionService by default.
In Spring 3.0.RC2, we can use ConversionServiceFactoryBean to create a ConversionService in Spring config. If people have a lot of Converter in the infrastructure libraries, they need to register all customized converters by themself, so we think it would be good if Converter itself can register itself.
Affects: 3.0 RC1, 3.0 RC2
4 votes, 8 watchers
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
spring-projects-issues commentedon Nov 24, 2009
Keith Donald commented
Related to this: some converters require a callback into the ConversionService. Some sort of conversion service aware concept would simplify the config of such converters.
spring-projects-issues commentedon Nov 24, 2009
Lance Arlaus commented
I ran into both these issues on my current project where I'm using the conversion service.
Here's the solution I came up with.
Notes
ConverterRegistry
interface support the extended converter registration methods?Code
spring-projects-issues commentedon Dec 8, 2009
Mark Kralj-Taylor commented
Could a JAR META-INF entry be used to identify converters?
This would allow infrastructure converters to be picked up in a zero touch way from Jars on the classpath.
Spring could even use the same mechanism for its default converters.
Or if auto registration from JAR META-INF scanning is seen as 'too-much magic',
then it could be turned on by a tag in config, in the same way that component scanning is.
spring-projects-issues commentedon Dec 22, 2009
Liu, Yinwei David commented
Hi,
We implement our Converter auto-registration by scanning JAR META-INF/services/spring.convert.Converters.
Auto Register Converters: All Converters will be registered to ConversionService by placing a text file at META-INF/services/spring.convert.Converters containing a list of fully qualified class name of each Converter on its own line. The CustomizedConversionServiceFactoryBean will return a ConversionService which contains all Converters registered in this way.
META-INF/services/spring.convert.Converters sample file:
spring.convert.TestConverterA
spring.convert.TestConverterB
Is it possible that Spring can provide a similar solution for Converters auto registration?
spring-projects-issues commentedon Nov 21, 2011
André Wolf commented
I found a very simple solution which seems to work fine for me. I just created a common super class
AutoRegisteredConverter
which registers itself in a@PostConstruct
method. TheConversionService
gets injected by Spring.For me it's a preferable solution, because it does not need a
BeanPostProcessor
(which are problematic for me in Unit Tests when using SpringJUnitClassRunner). Another advantage is, that each customConverter
has access to theConversionService
which is perfect for converting transitive object references.spring-projects-issues commentedon Jun 1, 2013
Kosta Krauth commented
A variation on Andre's approach, registering all the converters once the context has been loaded does the trick too. Didn't encounter any issues with the SpringJUnitClassRunner. With this approach, the Converters can be implemented in the standard way without any additional classes.
\
\
The approach of adding a component with the FactoryBean<ConversionService> interface that registers all Converters on itself works as well, as long as you don't autowire the ConversionService into Converters themselves. That scenario quickly descends into circular dependency hell.
bclozel commentedon Sep 20, 2021
This approach requires more fine-grained knowledge of the application.
I think this has been covered by Spring Boot for configuration properties and web data binding.
This looks like a general auto-configuration feature rather than a Spring Framework concern. I'm closing this issue as a result.