问题描述
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)
问题说明
查了一个下午,确定没有看懂这个错误栈说的是啥,谷歌百度都用上也没有具体的原因和解决方式,然后我就自己试,就找到原因了。但是为什么会报这个错,我还是无法解释各中原由,接下来我就只说我的错误在哪,我是怎么解决的吧。如果有大神知道是背后的原理机制,还请不吝赐教。
解决方式
该问题是我的项目中两处错误导致:
- @Transactional注解加错了地方,应该加到@Repository(给持久层做单元测试时是加到这里,真正的事务应该在service业务层控制),但是我给加到了配置类上了,跟@EnableTransactionManagement注解放一块了。
- 我的dao实现类,也就是@Repository上没有加@Transactional。
有@Transactional注解, 虽然加错了地方, 运行就不会报HibernateException: Could not obtain transaction-synchronized Session for current thread异常。但是如果没有给数据库操作加@Transactional注解以开启事务,就会导致上述异常了。