BeanCurrentlyInCreationException unresolvable circular reference

本文解析了在Spring和Hibernate整合测试中遇到的复杂异常,揭示了错误的根本原因在于@Transactional注解的误用和缺失。详细阐述了如何正确使用@Transactional,并强调了其在持久层和服务层的不同作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

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注解以开启事务,就会导致上述异常了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值