I am working with Spring Framework 4.0.7
About Formatters
For a Spring MVC application I have the following:
@Autowired
private Set<Formatter<?>> formatters;
@Override
public void addFormatters(FormatterRegistry registry) {
for(Formatter<?> formatter : formatters){
registry.addFormatter(formatter);
}
}
And works fine.
for Spring Web Flow I must add the following
@Bean
public DefaultFormattingConversionService conversionService(){
DefaultFormattingConversionService dfcs = new DefaultFormattingConversionService();
for(Formatter<?> formatter : formatters){
dfcs.addFormatter(formatter);
}
return dfcs;
}
And works fine
Until here, I can see in any Web forms (MVC & Flows) that when some data is submit or loaded the formatters
do their job.
I am with the impression DefaultFormattingConversionService can be used in standalone or for testing purposes (JUnit) to show some object formatted (decimal formats, i.e.: Double & BigDecimal).
A specialization of FormattingConversionService configured by default with converters and formatters appropriate for most applications.
Designed for direct instantiation but also exposes the static addDefaultFormatters(org.springframework.format.FormatterRegistry) utility method for ad hoc use against any FormatterRegistry instance, just as DefaultConversionService exposes its own addDefaultConverters method.
According with above, there is no a restriction about that only can be applied or used in a web environment.
For a new standalone application with the conversionService()
declared
I have the following:
@Component
public class PersonService {
private static final Logger logger = LoggerFactory.getLogger(PersonService.class);
public void printPerson(Person person){
logger.info("{}", person.toString());
}
}
In the Main
AnnotationConfigApplicationContext context = ...
Person person = PersonFactory.createPerson();
PersonService personService = context.getBean(PersonService.class);
personService.printPerson(person);
When I run the application, I can see and confirm that all the formatters have been loaded in the ApplicationContext, but when an object is printed none formatter according with the type (Double, BigDecimal) are executed.
What extra configuration is required? Is obvious that something is required
I am not sure if is possible, I need that when the .toString()
method is called, Spring in some way can be able to apply the formatters
for the Double BigDecimal and Date properties of the Entity.
It for a standalone application and for testing.