title: JSR-303 dependency injection and Hibernate
body: <p>Spring 3.0.2,
  Hibernate 3.5.0,
  Hibernate-Validator 4.0.2.GA</p>
  
  <p>I am trying to inject Spring dependencies into a ConstraintValidator using:</p>
  
  <pre><code>@PersistenceContext
  private EntityManager entityManager;
  </code></pre>
  
  <p>I have configured the application context with:</p>
  
  <pre><code>&lt;bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/&gt;
  </code></pre>
  
  <p>Which, according to the Spring documentation, should allow “custom ConstraintValidators to benefit from dependency injection like any other Spring bean”</p>
  
  <p>Within the debugger I can see Spring calling getBean to create the ConstraintValidator. Later when flush triggers the preInsert, a different ConstraintValidator is created and called. The problem is the EntityManager is null within this new ConstraintValidator. I’ve tried injecting other dependencies within the ConstraintValidator and these are always null. </p>
  
  <p>Does anyone know if it is possible to inject dependencies into a ConstraintValidator?</p>