title: How to Inject the EntityManager in an Hibernate Interceptor Bean using Spring?
body: <p>I need to create an Hibernate interceptor that must have access to the entity manager. The problem is that when I am defining how the EntityManagerFactory is going to be created using the XML config file in Spring for defining the entityManagerFactory bean I have to tell the entity manage which interceptor bean it must use. The thing is that my Interceptor bean has an injected entity manager field defined using </p>
  
  <pre><code>@PersistenceContext private EntityManager entityManager;
  </code></pre>
  
  <p>When I do this Spring throws the following exception: </p>
  
  <blockquote>
    <p>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ar.com.zauber.commons.repository.utils.ConfigurableHibernatePersistence#50d17ec3' defined in class path resource [ar/com/xxx/impl/config/persistence/persistence-impl-xxx-spring.xml]: Cannot resolve reference to bean 'interceptor' while setting bean property 'interceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'interceptor': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'entityManagerFactory': FactoryBean which is currently in creation returned null from getObject</p>
  </blockquote>
  
  <p>The problem is that the entity manager could not be injected because the entity manager factory is being created.</p>
  
  <p>Any idea how can I solve this problem?</p>