简体   繁体   English

如何使用Spring在Hibernate Interceptor Bean中注入EntityManager?

[英]How to Inject the EntityManager in an Hibernate Interceptor Bean using Spring?

I need to create an Hibernate interceptor that must have access to the entity manager. 我需要创建一个必须具有访问实体管理器的Hibernate拦截器。 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. 问题是,当我定义如何使用Spring中的XML配置文件创建EntityManagerFactory来定义entityManagerFactory bean时,我必须告诉实体管理它必须使用哪个拦截器bean。 The thing is that my Interceptor bean has an injected entity manager field defined using 问题是我的Interceptor bean有一个使用注入的实体管理器字段

@PersistenceContext private EntityManager entityManager;

When I do this Spring throws the following exception: 当我这样做时,Spring抛出以下异常:

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'; 引起:org.springframework.beans.factory.BeanCreationException:创建名为'ar.com.zauber.commons.repository.utils.ConfigurableHibernatePersistence#50d17ec3'的bean在类路径资源中定义时出错[ar / com / xxx / impl / config /persistence/persistence-impl-xxx-spring.xml]:设置bean属性'interceptor'时无法解析对bean'interceptor'的引用; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'interceptor': Injection of persistence dependencies failed; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'interceptor'的bean时出错:持久性依赖项的注入失败; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'entityManagerFactory': FactoryBean which is currently in creation returned null from getObject 嵌套异常是org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为'entityManagerFactory'的bean时出错:当前处于创建状态的FactoryBean从getObject返回null

The problem is that the entity manager could not be injected because the entity manager factory is being created. 问题是无法注入实体管理器,因为正在创建实体管理器工厂。

Any idea how can I solve this problem? 不知道怎样才能解决这个问题?

Use depends-on (XML version): 使用依赖(XML版本):

<bean id="interceptor"
    class="YourHibernateInterceptor" depends-on="entityManagerFactory"/>

Or @DependsOn (Annotation version): @DependsOn (注释版本):

@DependsOn("entityManagerFactory")
public class YourHibernateInterceptor{
  // ...
}

Reference: 参考:


If this doesn't work because it's a chicken / egg problem (EntityManagerFactory depends on SessionFactory, SessionListener depends on EntityManagerFactory, you can mark your SessionListener as either ApplicationContextAware or ApplicationListener<ContextRefreshedEvent> and manually wire the EntityManager : 如果这不起作用,因为它是鸡/蛋问题(EntityManagerFactory依赖于SessionFactory,SessionListener依赖于EntityManagerFactory,您可以将SessionListener标记为ApplicationContextAwareApplicationListener<ContextRefreshedEvent>并手动连接EntityManager

this.entityManager = context.getBean(EntityManager.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM