Spring boot 无法在我的Spring引导演示中自动连接sessionFactory。创建bean时出错";sessionFactory";:不可解循环参考
谢谢你帮我弄明白 我从一个简单的“hello world”演示开始,一开始效果很好。 然后我尝试在我的BaseDao类中注入hibernate的sessionFactory,使用以下yml配置:Spring boot 无法在我的Spring引导演示中自动连接sessionFactory。创建bean时出错";sessionFactory";:不可解循环参考,spring-boot,circular-reference,sessionfactory,Spring Boot,Circular Reference,Sessionfactory,谢谢你帮我弄明白 我从一个简单的“hello world”演示开始,一开始效果很好。 然后我尝试在我的BaseDao类中注入hibernate的sessionFactory,使用以下yml配置: spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/myboot1?useUnicode=true&characterEncoding=UT
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/myboot1?useUnicode=true&characterEncoding=UTF-8
username: root
password: kenny
jpa:
properties:
hibernate:
current_session_context_class: org.springframework.orm.hibernate5.SpringSessionContext
这是我的配置类:
@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement
public class HibernateConfig {
@Bean
public SessionFactory sessionFactory(EntityManagerFactory factory) {
if (factory.unwrap(SessionFactory.class) == null) {
throw new NullPointerException("noooooooooooo!");
}
return factory.unwrap(SessionFactory.class);
}
}
我得到了这个错误:
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sessionFactory':
Requested bean is currently in creation: Is there an unresolvable circular reference?
The dependencies of some of the beans in the application context form a cycle:
***************************
APPLICATION FAILED TO START
***************************
Description:
helloController (field com.kennywgx.boot2.service.UserService com.kennywgx.boot2.controller.HelloController.service)
↓
userService (field private com.kennywgx.boot2.dao.UserDao com.kennywgx.boot2.service.UserService.dao)
↓
userDao (field private org.hibernate.SessionFactory com.kennywgx.boot2.dao.BaseDao.sessionFactroy)
┌─────┐
| sessionFactory defined in class path resource [com/kennywgx/boot2/HibernateConfig.class]
└─────┘
我不明白为什么SpringContainer循环创建它。有人知道为什么吗
UserDao.java的代码
@Repository
public class UserDao extends BaseDao {
}
BaseDao
@Repository
public class BaseDao {
@Autowired
private SessionFactory sessionFactory;
public Session getCurrSession() {
return sessionFactory.getCurrentSession();
}
}
我既不能注入SessionFactory也不能注入EntityManagerFactory。EntityRangerFactory为空