0

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.

Community
  • 1
  • 1
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
  • A `Formatter` will only be used when using Spring to output a field or object. You are just doing a `toString()` which doesn't do nor knows anything about formatters. – M. Deinum Oct 08 '14 at 06:24
  • I understand, for Console purposes, Spring what offers to do an output? Thanks – Manuel Jordan Oct 08 '14 at 13:04

1 Answers1

0

I have the same problem. I realize that it's inconvenience to test formatter and convertor without MVC but it turn out more simple than i think.

I hope my way also work for you.

For example:

I have a simple converter. That can convert from username to User object.

String userName -> User Object and vice versa

The solution for test it:

@Autowired
ConversionService conversionService;

@Test
public void testConversionService(){
    assertThat(conversionService,not(equalTo(null)));
    User user = conversionService.convert("Huy", User.class);
    assertThat(user,not(equalTo(null)));
    assertThat(user.getUserName(),equalTo("Huy"));
}

Hope that help!

Huy Nguyen
  • 1,931
  • 1
  • 11
  • 11