title: Spring-Boot How to properly inject javax.validation.Validator
body: <p>I've got a problem injecting the <code>Validator</code> into the spring application bean when attempting to validate a model using JSR-303 (hibernate-validator) </p>
  
  <p>My main configuration class is:</p>
  
  <pre><code>@EnableAutoConfiguration
  @EnableWebMvc // &lt;---
  @EnableJpaRepositories("com.example")
  @EntityScan("com.example")
  public class MainConfiguration {
  </code></pre>
  
  <p>According to the javadocs:</p>
  
  <pre><code>/**
   * Provide a custom {@link Validator} instead of the one created by default.
   * The default implementation, assuming JSR-303 is on the classpath, is:
   * {@link org.springframework.validation.beanvalidation.LocalValidatorFactoryBean}.
   * Leave the return value as {@code null} to keep the default.
   */
  Validator getValidator();
  </code></pre>
  
  <p>Hibernate-validator is on the classpath.
  I'm trying to inject it into the Repository:</p>
  
  <pre><code>@Repository
  public class UserRepositoryImpl implements UserRepositoryCustom    {
  
      @Autowired
      private Validator validator;
  </code></pre>
  
  <p>Exception being thrown:</p>
  
  <pre><code> No qualifying bean of type [javax.validation.Validator] found for dependency:
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>The partial work-around for this is to define this in the main configuration class:</p>
  
  <pre><code>  @Bean
      public Validator validator() {
  
          return new org.springframework.validation.beanvalidation.LocalValidatorFactoryBean();
      }
  </code></pre>
  
  <p>But integration tests (the ones which require <code>org.springframework.test.context.web.WebAppConfiguration;</code> annotation and use validation logic) fail.</p>