title: Dependency Injection in JSR-303 Constraint Validator with Spring fails
body: <p>I have the same problem as <a href="https://stackoverflow.com/questions/13350537/inject-service-in-constraintvalidator-bean-validator-jsr-303-spring">here</a> and <a href="https://stackoverflow.com/questions/17083625/dependency-injection-in-jsr-303-constraintvalidator">here</a> but couldn't find a solution yet.</p>
  
  <p>So my sample test project will show the whole relevant configuration and code:</p>
  
  <p>Constraint annotation:</p>
  
  <pre><code>@Target({ ElementType.METHOD, ElementType.FIELD })
  @Retention(RetentionPolicy.RUNTIME)
  @Constraint(validatedBy = FooValidator.class)
  public @interface FooValid {
  
      String message();
  
      Class&lt;?&gt;[] groups() default {};
  
      Class&lt;? extends Payload&gt;[] payload() default {};
  }
  </code></pre>
  
  <p>Annotated PoJo:</p>
  
  <pre><code>public class Foo {
  
      @FooValid(message = "Test failed")
      private Integer test;
      [...]
  }
  </code></pre>
  
  <p>Annotated Service with @Validated:</p>
  
  <pre><code>@Service
  @Validated
  public class FooService {
  
      private final Test test;
  
      @Autowired
      public FooService(final Test test) {
          this.test = test;
      }
  
      public void foo(@Valid final Foo foo) {
          this.test.test(foo);
      }
  }
  </code></pre>
  
  <p>JSR-303 ConstraintValidator:</p>
  
  <pre><code>public class FooValidator implements ConstraintValidator&lt;FooValid, Integer&gt; {
  
      @Autowired
      private ValidationService validationService;
  
      @Override
      public void initialize(final FooValid constraintAnnotation) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public boolean isValid(final Integer value, final ConstraintValidatorContext context) {
          // this.validationService is always NULL!
          Assert.notNull(this.validationService, "the validationService must not be null");
          return false;
      }
  
  }
  </code></pre>
  
  <p>Injected ValidationService:</p>
  
  <pre><code>@Service
  public class ValidationService {
  
      public void test(final Foo foo) {
          System.out.println(foo);
      }
  }
  </code></pre>
  
  <p>Spring boot application and configuration:</p>
  
  <pre><code>@Configuration
  @ComponentScan
  @EnableAutoConfiguration
  public class Application {
  
      public static void main(final String[] args) {
          final ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
          final FooService service = context.getBean(FooService.class);
          service.foo(new Foo());
      }
  
      @Bean
      public static LocalValidatorFactoryBean validatorFactory() {
          return new LocalValidatorFactoryBean();
      }
  
      @Bean
      public static MethodValidationPostProcessor validationPostProcessor() {
          return new MethodValidationPostProcessor();
      }
  
  }
  </code></pre>
  
  <p>relevant maven pom:</p>
  
  <pre><code>&lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;1.1.9.RELEASE&lt;/version&gt;
      &lt;relativePath/&gt;
  &lt;/parent&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  
  &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;start-class&gt;demo.Application&lt;/start-class&gt;
      &lt;java.version&gt;1.7&lt;/java.version&gt;
  &lt;/properties&gt;
  
  &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p>I'm using the <strong><em>LocalValidatorFactoryBean</em></strong> with the default <strong><em>SpringConstraintValidatorFactory</em></strong>.
  But why the dependency injection is not working in the ConstraintValidator and the <em>ValidationService</em> could not be autowired?</p>
  
  <p>By the way if I don't use <em>@Validated</em> at the service, inject in opposite the spring or javax Validator interface and call manually "<em>validator.validate</em>" the dependency injection will work. But I don't want to call the validate method in every service manually.</p>
  
  <p>Many thanks for help :)</p>