Thank you for helping me figure it out.
I started with a simple "hello world" demo, and it worked fine at the beginning. then i tried to inject hibernate's sessionFactory in my BaseDao class, using this yml config:
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
and this is my configuration class:
@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);
}
}
and i got this Error:
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]
└─────┘
I dont understand why spring container created it circularly. Somebody know why?
code of UserDao.java
@Repository
public class UserDao extends BaseDao {
}
BaseDao
@Repository
public class BaseDao {
@Autowired
private SessionFactory sessionFactory;
public Session getCurrSession() {
return sessionFactory.getCurrentSession();
}
}