BeanCurrentlyInCreationException unresolvable circular reference

问题描述

spring和hibernate整合后,对数据库的增删改查进行测试时,运行报错如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userHibernateDao’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory org.myexample.dao.AbstractDao.sessionFactory; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘sessionFactory’ defined in org.myexample.dao.config.HibernateConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [javax.sql.DataSource]: Error creating bean with name ‘dataSource’ defined in org.myexample.dao.config.HibernateConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method ‘dataSource’ threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘transactionManager’ defined in org.myexample.dao.config.HibernateConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource’ defined in org.myexample.dao.config.HibernateConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method ‘dataSource’ threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘transactionManager’ defined in org.myexample.dao.config.HibernateConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.myexample.TestUserDaoHibernateImpl.init(TestUserDaoHibernateImpl.java:24)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory org.myexample.dao.AbstractDao.sessionFactory; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘sessionFactory’ defined in org.myexample.dao.config.HibernateConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [javax.sql.DataSource]: Error creating bean with name ‘dataSource’ defined in org.myexample.dao.config.HibernateConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method ‘dataSource’ threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘transactionManager’ defined in org.myexample.dao.config.HibernateConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource’ defined in org.myexample.dao.config.HibernateConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method ‘dataSource’ threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘transactionManager’ defined in org.myexample.dao.config.HibernateConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.myexample.TestUserDaoHibernateImpl.init(TestUserDaoHibernateImpl.java:24)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘sessionFactory’ defined in org.myexample.dao.config.HibernateConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [javax.sql.DataSource]: Error creating bean with name ‘dataSource’ defined in org.myexample.dao.config.HibernateConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method ‘dataSource’ threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘transactionManager’ defined in org.myexample.dao.config.HibernateConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource’ defined in org.myexample.dao.config.HibernateConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method ‘dataSource’ threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘transactionManager’ defined in org.myexample.dao.config.HibernateConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.myexample.TestUserDaoHibernateImpl.init(TestUserDaoHibernateImpl.java:24)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource’ defined in org.myexample.dao.config.HibernateConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method ‘dataSource’ threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘transactionManager’ defined in org.myexample.dao.config.HibernateConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.myexample.TestUserDaoHibernateImpl.init(TestUserDaoHibernateImpl.java:24)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method ‘dataSource’ threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘transactionManager’ defined in org.myexample.dao.config.HibernateConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.myexample.TestUserDaoHibernateImpl.init(TestUserDaoHibernateImpl.java:24)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘transactionManager’ defined in org.myexample.dao.config.HibernateConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.myexample.TestUserDaoHibernateImpl.init(TestUserDaoHibernateImpl.java:24)
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘sessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.myexample.TestUserDaoHibernateImpl.init(TestUserDaoHibernateImpl.java:24)

问题说明

查了一个下午,确定没有看懂这个错误栈说的是啥,谷歌百度都用上也没有具体的原因和解决方式,然后我就自己试,就找到原因了。但是为什么会报这个错,我还是无法解释各中原由,接下来我就只说我的错误在哪,我是怎么解决的吧。如果有大神知道是背后的原理机制,还请不吝赐教。

解决方式

该问题是我的项目中两处错误导致:

  1. @Transactional注解加错了地方,应该加到@Repository(给持久层做单元测试时是加到这里,真正的事务应该在service业务层控制),但是我给加到了配置类上了,跟@EnableTransactionManagement注解放一块了。
  2. 我的dao实现类,也就是@Repository上没有加@Transactional。

有@Transactional注解, 虽然加错了地方, 运行就不会报HibernateException: Could not obtain transaction-synchronized Session for current thread异常。但是如果没有给数据库操作加@Transactional注解以开启事务,就会导致上述异常了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: "Is there an unresolvable circular reference?"这个问题是关于SpringBoot启动报错的一个常见问题。当看到"Requested bean is currently in creation: Is there an unresolvable circular reference?"这个错误信息时,它是在告诉我们存在一个无法解决的循环引用。循环引用是指两个或多个bean之间相互依赖,但又无法解决依赖关系,导致循环引用。具体解决方法有两种:一种是检查代码中是否存在循环引用的问题,将循环引用解决掉;另一种是在配置文件application.yml中添加allow-circular-references: true属性,允许存在循环引用。但是需要注意的是,这种方式只适用于比较老旧的项目且循环依赖比较多的情况下才使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Requested bean is currently in creation: Is there an unresolvable circular reference? 原因之一(java...](https://blog.csdn.net/sinat_37792529/article/details/131573638)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Requested bean is currently in creation: Is there an unresolvable circular reference?](https://blog.csdn.net/WY_cat/article/details/102929469)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值