title: How apply a Spring Formatter<T> in a standalone or testing application
body: <p>I am working with Spring Framework 4.0.7</p>
  <p>About <code>Formatters</code></p>
  <p>For a Spring MVC application I have the following:</p>
  <pre><code>@Autowired
  private Set&lt;Formatter&lt;?&gt;&gt; formatters;
  
  @Override
  public void addFormatters(FormatterRegistry registry) {
      
      for(Formatter&lt;?&gt; formatter : formatters){
          registry.addFormatter(formatter);
      }
      
  }
  </code></pre>
  <p>And works fine.</p>
  <p>for Spring Web Flow I must add the following</p>
  <pre><code>@Bean
  public DefaultFormattingConversionService conversionService(){
      DefaultFormattingConversionService dfcs = new DefaultFormattingConversionService();
      for(Formatter&lt;?&gt; formatter : formatters){
          dfcs.addFormatter(formatter);
      }
      return dfcs;
  }
  </code></pre>
  <p>And works fine</p>
  <p>Until here, I can see in any Web forms (MVC &amp; Flows) that when some data is submit or loaded the <code>formatters</code> do their job.</p>
  <p>I am with the impression <a href="http://docs.spring.io/spring/docs/4.0.x/javadoc-api/org/springframework/format/support/DefaultFormattingConversionService.html" rel="nofollow noreferrer">DefaultFormattingConversionService</a> can be used in standalone or for testing purposes (JUnit) to show some object formatted (decimal formats, i.e.: Double &amp; BigDecimal).</p>
  <blockquote>
  <p>A specialization of FormattingConversionService configured by default with converters and formatters appropriate for most applications.</p>
  <p>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.</p>
  </blockquote>
  <p>According with above, there is no a restriction about that only can be applied or used in a web environment.</p>
  <p>For a new standalone application with the <code>conversionService()</code> declared</p>
  <p>I have the following:</p>
  <pre><code>@Component
  public class PersonService {
  
      private static final Logger logger = LoggerFactory.getLogger(PersonService.class);
      
      public void printPerson(Person person){
          logger.info(&quot;{}&quot;, person.toString());
      }
      
  }
  </code></pre>
  <p>In the Main</p>
  <pre><code>AnnotationConfigApplicationContext context = ...
          
  Person person = PersonFactory.createPerson();
          
  PersonService personService = context.getBean(PersonService.class); 
  personService.printPerson(person);
  </code></pre>
  <p>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.</p>
  <p>What extra configuration is required? Is obvious that something is required</p>
  <p>I am not sure if is possible, I need that when the <code>.toString()</code> method is called, Spring in some way can be able to apply the <code>formatters</code> for the Double BigDecimal and Date properties of the Entity.
  It for a standalone application and for testing.</p>