title: Register Spring Converter Programmatically in Spring Boot
body: <p>I want to register a Spring Converter in a Spring Boot project programmatically. In past Spring projects I've done it in XML like this... </p>
  
  <pre><code>&lt;!-- Custom converters to allow automatic binding from Http requests parameters to objects --&gt;
  &lt;!-- All converters are annotated w/@Component --&gt;
  &lt;bean id="conversionService"
        class="org.springframework.context.support.ConversionServiceFactoryBean"&gt;
      &lt;property name="converters"&gt;
          &lt;list&gt;
              &lt;ref bean="stringToAssessmentConverter" /&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>I'm trying to figure out how to do in Spring Boot's SpringBootServletInitializer</p>
  
  <p><strong>Update:</strong> I've made a little progress by passing the StringToAssessmentConverter as an argument to <code>getConversionService</code>, but now I'm getting a <code>"No default constructor found"</code> error for the StringToAssessmentConverter class. I'm not sure why Spring is not seeing the @Autowired constructor.</p>
  
  <pre><code>@SpringBootApplication
  public class Application extends SpringBootServletInitializer {
  
      ...
  
      @Bean(name="conversionService")
      public ConversionServiceFactoryBean getConversionService(StringToAssessmentConverter stringToAssessmentConverter) {
          ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
  
          Set&lt;Converter&gt; converters = new HashSet&lt;&gt;();
  
          converters.add(stringToAssessmentConverter);
  
          bean.setConverters(converters);
          return bean;
      }
  }  
  </code></pre>
  
  <p>Here's the code for the Converter... </p>
  
  <pre><code> @Component
   public class StringToAssessmentConverter implements Converter&lt;String, Assessment&gt; {
  
       private AssessmentService assessmentService;
  
       @Autowired
       public StringToAssessmentConverter(AssessmentService assessmentService) {
           this.assessmentService = assessmentService;
       }
  
       public Assessment convert(String source) {
           Long id = Long.valueOf(source);
           try {
               return assessmentService.find(id);
           } catch (SecurityException ex) {
               return null;
           }
       }
   }
  </code></pre>
  
  <p>Full Error</p>
  
  <pre><code>Failed to execute goal org.springframework.boot:spring-boot-maven-
  plugin:1.3.2.RELEASE:run (default-cli) on project yrdstick: An exception 
  occurred while running. null: InvocationTargetException: Error creating 
  bean with name
  'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPo
  stProcessor': Invocation of init method failed; nested exception is 
  org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
  creating bean with name 'conversionService' defined in 
  me.jpolete.yrdstick.Application: Unsatisfied dependency expressed through 
  constructor argument with index 0 of type 
  [me.jpolete.yrdstick.websupport.StringToAssessmentConverter]: : Error 
  creating bean with name 'stringToAssessmentConverter' defined in file 
  [/yrdstick/target/classes/me/jpolete/yrdstick/websupport
  /StringToAssessmentConverter.class]: Instantiation of bean failed; nested 
  exception is org.springframework.beans.BeanInstantiationException: Failed 
  to instantiate 
  [me.jpolete.yrdstick.websupport.StringToAssessmentConverter]: No default 
  constructor found; nested exception is java.lang.NoSuchMethodException: 
  me.jpolete.yrdstick.websupport.StringToAssessmentConverter.&lt;init&gt;(); 
  nested exception is 
  org.springframework.beans.factory.BeanCreationException: Error creating 
  bean with name 'stringToAssessmentConverter' defined in file [/yrdstick
  /dev/yrdstick/target/classes/me/jpolete/yrdstick/websupport
  /StringToAssessmentConverter.class]: Instantiation of bean failed; nested 
  exception is org.springframework.beans.BeanInstantiationException: Failed 
  to instantiate 
  [me.jpolete.yrdstick.websupport.StringToAssessmentConverter]: No default 
  constructor found; nested exception is java.lang.NoSuchMethodException: 
  me.jpolete.yrdstick.websupport.StringToAssessmentConverter.&lt;init&gt;()
  </code></pre>