我需要创建一个必须具有访问实体管理器的Hibernate拦截器 . 问题是,当我定义如何使用Spring中的XML配置文件创建EntityManagerFactory来定义entityManagerFactory bean时,我必须告诉实体管理它必须使用哪个拦截器bean . 问题是我的Interceptor bean有一个使用注入的实体管理器字段
@PersistenceContext private EntityManager entityManager;
当我这样做时,Spring抛出以下异常:
引起: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'的引用;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'interceptor'的bean时出错:持久性依赖项的注入失败;嵌套异常是org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为'entityManagerFactory'的bean时出错:当前处于创建状态的FactoryBean从getObject返回null
问题是无法注入实体管理器,因为正在创建实体管理器工厂 .
不知道怎样才能解决这个问题?
1 回答
使用依赖(XML版本):
或
@DependsOn
(注释版):Reference:
Using depends-on
@DependsOn
如果这不是一个鸡/蛋问题(EntityManagerFactory依赖于SessionFactory,SessionListener依赖于EntityManagerFactory,您可以将SessionListener标记为ApplicationContextAware或ApplicationListener<ContextRefreshedEvent>并手动连接
EntityManager
: