gpt4 book ai didi

hibernate 拦截器循环依赖的Spring配置

转载 作者:行者123 更新时间:2023-12-05 07:45:27 25 4
gpt4 key购买 nike

我正在尝试实现 hibernate 拦截以在基于 spring 的项目中创建日志表,但我在配置文件上苦苦挣扎。
一切都工作得很好,但是拦截器永远不会触发。

我是我的拦截器,我有 @Autowired 我的 Dao,因为我需要访问它以在 LogTable 中保存新输入。所有 Daos 在参数中都有 session 工厂,所以当我尝试添加

<property name="entityInterceptor">
<bean class="com.mypackage.MyLoggingInterceptor"></bean>
</property>

我对 sessionFactory bean 有循环依赖:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [URL]: Cannot resolve reference to bean 'MyLoggingInterceptor' while setting bean property 'entityInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyLoggingInterceptor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.mypackage.UserLogDao com.mypackage.MyLoggingInterceptor.userLogDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserLogDao' defined in URL [URL]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sessionFactory': FactoryBean which is currently in creation returned null from getObject

如果我不添加 entityInterceptor 属性,我的程序可以正常工作,但永远不会调用拦截器。我应该怎么做才能解决这种循环依赖?

最佳答案

可以通过使用@Lazy注释@Autowired属性来实现循环依赖。

关于 hibernate 拦截器循环依赖的Spring配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41619397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com