<img height="1" width="1" style="display:none;" alt="" src="https://px.ads.linkedin.com/collect/?pid=299788&amp;fmt=gif">

Spring 4.2.0 Removal of installFormatters

Software Development, Software Solutions, Java

By Ryan Peterson

spring_logo
This blog entry is about something I came across recently in a project I've been on. During the upgrade to Spring 4.2.0.release, the long-deprecated method installFormatters from FormattingConversionServiceFactoryBean was removed.
This caused problems, as the project relied on the installFormatters method to register a custom ConverterFactory (from String to domain object) with Spring.
Here's a snippet of the custom class containing the installFormatters method:

public class ApplicationConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean {
@Override
protected void installFormatters(FormatterRegistry registry) {
super.installFormatters(registry);
// Register application converters and formatters
registry.addConverterFactory(new StringToEntityConverterFactory());
registry.addConverterFactory(new SomeOtherCustomFactory());
}
}
To register it for use in the app, it was declared as a bean thusly:
<bean class="com.somecompany.dont.run.with.scissors.ApplicationConversionServiceFactoryBean" id="applicationConversionService"/>
This of course throws up the errors on startup with 4.2.0.RELEASE:
The method installFormatters(FormatterRegistry) of type ApplicationConversionServiceFactoryBean must override or implement a supertype method
The method installFormatters(FormatterRegistry) is undefined for the type FormattingConversionServiceFactoryBean
This was where I had to dig/fight for a way around the problem. The documentation for FormattingConversionServiceFactoryBean gives some information, but nothing I could easily implement.
After much experimentation, searching, and head scratching, I realized it was easier to completely eliminate the custom implementation to register the ConverterFactory, and instead inject it via xml config using the default Spring FormattingConversionServiceFactoryBean:
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="applicationConversionService">
<property name="converters">
<set>
<bean class="com.somecompany.dont.run.with.scissors.converters.StringToEntityConverterFactory"/>
</set>
</property>
</bean>
This was the easiest/first method I found would properly register the ConverterFactory so that Spring can utilize it.  This solution requires that the ConverterFactory be annotated as a @Component, so Spring properly initializes it.  FormattingConversionServiceFactoryBean takes any converters in this set, so you could register others you'd like to use as well.  I haven't tried, but I assume registering Formatters would be much the same.
I hope this helps if someone else runs into this problem upgrading their Spring implementation.

 

TAGS: Software Development, Software Solutions, Java

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Subscribe to Our Newsletter

Recent Blog Posts