title: Java Config equivalent for conversionService / FormattingConversionServiceFactoryBean
body: <p>I have an entity that contains another entity, as follows:</p>
  
  <pre><code>public class Order {
  
      @Id
      private int id;
  
      @NotNull
      private Date requestDate;
  
      @NotNull
      @ManyToOne(cascade=CascadeType.ALL)
      @JoinColumn(name="order_type_id")
      private OrderType orderType;
  }
  
  public class OrderType {
  
      @Id
      private int id;
  
      @NotNull
      private String name;
  }
  </code></pre>
  
  <p>I have a Spring MVC form where a user can submit a new order; the fields they have to fill in are the Request Date and select an Order Type (which is a drop-down).</p>
  
  <p>I am using Spring Validation to validate the form input which fails as it is trying to convert an orderType.id to an OrderType.</p>
  
  <p>I have written a custom converter to convert an orderType.id into an OrderType object:</p>
  
  <pre><code>public class OrderTypeConverter implements Converter&lt;String, OrderType&gt; {
  
      @Autowired
      OrderTypeService orderTypeService;
  
      public OrderType convert(String orderTypeId) {
  
          return orderTypeService.getOrderType(orderTypeId);
      }
  }
  </code></pre>
  
  <p>My problem is I don't know how to register this converter with Spring using java config. The XML equivalent I've found (from <a href="https://stackoverflow.com/questions/13480694/dropdown-value-binding-in-spring-mvc">Dropdown value binding in Spring MVC</a>) is:</p>
  
  <pre><code>&lt;mvc:annotation-driven conversion-service="conversionService"/&gt;
  
  &lt;bean id="conversionService"
      class="org.springframework.format.support.FormattingConversionServiceFactoryBean"&gt;
      &lt;property name="converters"&gt;
         &lt;list&gt;
            &lt;bean class="OrderTypeConverter"/&gt;
         &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>From searching the web I can't seem to find a java config equivalent - can someone please help me out?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have added the OrderTypeConvertor to the WebMvcConfigurerAdapter as follows:</p>
  
  <pre><code>public class MvcConfig extends WebMvcConfigurerAdapter{
  
      ...
  
      @Override
      public void addFormatters(FormatterRegistry registry) {
          registry.addConverter(new OrderTypeConvertor());
      }
  }
  </code></pre>
  
  <p>However I get a null pointer exception in OrderTypeConvertor as orderTypeService is null, presumably because it is autowired and I've used the new keyword above. Some further help would be appreciated.</p>