title: Use Spring dependency injection on a custom Hibernate validator
body: <p>I want to create my own Hibernate custom Validator and what I would like  to do is to add custom logic based on some information gathered from a different context (a different Spring bean)</p>
  
  <p>On my custom implementation, I have tried to both add a constructor and define the validator in a spring bean, or to use the Autowire annotation and none of them worked</p>
  
  <p>Autowire example:</p>
  
  <pre><code>public class MyCustomValidator implements ConstraintValidator&lt;CustomConstraint, String&gt; {
      @Autowired
      private MyCustomChecker customChecker;
  
      @Override
      public void initialize(CustomConstraint constraintAnnotation) {
      }
  
      @Override
      public boolean isValid(String value, ConstraintValidatorContext context) {
          if (null == value) {
              return true;
          }
  
          //get user authenticated properties to perform validation based on the user
          AuthenticatedIdentity identity = Context.getAuthenticatedIdentity();
          return customChecker.isWhitelisted(identity);
      }
  }
  </code></pre>
  
  <p>Constructor example:</p>
  
  <pre><code>public class MyCustomValidator implements ConstraintValidator&lt;CustomConstraint, String&gt; {
  
      private MyCustomChecker customChecker;
  
      public MetricDataSizeValidator(MyCustomChecker customChecker) {
          this.customChecker = customChecker;
      }
  
      @Override
      public void initialize(CustomConstraint constraintAnnotation) {
      }
  
      @Override
      public boolean isValid(String value, ConstraintValidatorContext context) {
          if (null == value) {
              return true;
          }
  
          //get user authenticated properties to perform validation based on the user
          AuthenticatedIdentity identity = Context.getAuthenticatedIdentity();
          return customChecker.isWhitelisted(identity);
      }
  }
  </code></pre>
  
  <p>I've read around on the <a href="https://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html/validator-integration.html#d0e5117" rel="nofollow noreferrer">official Hibernate doc</a> but that doesn't quite answer my question.</p>
  
  <p>I am pretty sure this is a common issue when you want to validate based on some information based from a different context, however I didn't find around an answer for this.</p>
  
  <p>My app is using Spring DI, where my bean is already initialized like this </p>
  
  <pre><code>&lt;bean id="customChecker" class="com.mycomp.CustomChecker"&gt;
      &lt;constructor-arg&gt;
          &lt;value&gt;arg&lt;/value&gt;
      &lt;/constructor-arg&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Is there any example around of how to achieve this? </p>
  
  <p><strong>Update</strong></p>
  
  <p>If I configure my Validator to be:</p>
  
  <pre><code>&lt;bean id="validatorFactory" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/&gt;
  &lt;bean id="validator" factory-bean="validatorFactory"
        factory-method="getValidator" /&gt;
  </code></pre>
  
  <p>Now I can see the validation is wired up correctly using spring beans. However, now I'm doubtful whether I'm using HibernateValidator (I think I'm not). Is there any way to achieve the same but configure HibernateValidator factory to use Spring beans?</p>