application context中的一些bean的依赖关系形成一个循环:session factory creating
The dependencies of some of the beans in the application context form a cycle: session factory creating
我是 Spring 框架的新手。我有一个 spring 引导应用程序(mvc、hibernate),我正在尝试获取会话工厂,但出现错误
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| securityConfig defined in file [mySrc\springBootApp\config\SecurityConfig.class]
↑ ↓
| userDetailsServiceImpl defined in file [mySrc/springBootApp\service\impl\UserDetailsServiceImpl.class]
↑ ↓
| userDAO defined in me.kqlqk.springBootApp.DAO.UserDAO defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration
↑ ↓
| (inner bean)#2b6a0ea9
└─────┘
Action:
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
我的配置class:
@Configuration
@EnableWebSecurity
@EnableTransactionManagement
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final UserDetailsService userDetailsService;
private Environment env;
@Autowired
public SecurityConfig(@Qualifier("userDetailsServiceImpl") UserDetailsService userDetailsService, Environment env) {
this.userDetailsService = userDetailsService;
this.env = env;
}
//configuration here
@Bean("entityManagerFactory")
@Autowired
public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
Properties properties = new Properties();
properties.put("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
properties.put("hibernate.show_sql", env.getProperty("spring.jpa.show-sql"));
properties.put("current_session_context_class", env.getProperty("spring.jpa.properties.hibernate.current_session_context_class"));
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setPackagesToScan("me.kqlqk.springBootApp.models");
sessionFactory.setHibernateProperties(properties);
return sessionFactory;
}
}
我使用@Bean("entityManagerFactory") 因为没有它我得到了
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in me.kqlqk.springBootApp.service.impl.UserDetailsServiceImpl required a bean named 'entityManagerFactory' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Qualifier(value="userDetailsServiceImpl")
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
和我的 userDetailsServiceImpl class:
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
private UserDAO userDAO;
@Autowired
public UserDetailsServiceImpl(UserDAO userDAO) {
this.userDAO = userDAO;
}
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
User user = userDAO.getByEmail(email);
Set<GrantedAuthority> authorities = new HashSet<>();
user.getRoles().forEach(role -> authorities.add(new SimpleGrantedAuthority(role.getName())));
return new org.springframework.security.core.userdetails.User(
user.getEmail(),
user.getPassword(),
authorities);
}
}
那么,我该如何配置会话工厂或者我的代码哪里有问题?
所以,我只是决定使用@Transactional 而不需要 sessionFactory
我是 Spring 框架的新手。我有一个 spring 引导应用程序(mvc、hibernate),我正在尝试获取会话工厂,但出现错误
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| securityConfig defined in file [mySrc\springBootApp\config\SecurityConfig.class]
↑ ↓
| userDetailsServiceImpl defined in file [mySrc/springBootApp\service\impl\UserDetailsServiceImpl.class]
↑ ↓
| userDAO defined in me.kqlqk.springBootApp.DAO.UserDAO defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration
↑ ↓
| (inner bean)#2b6a0ea9
└─────┘
Action:
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
我的配置class:
@Configuration
@EnableWebSecurity
@EnableTransactionManagement
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final UserDetailsService userDetailsService;
private Environment env;
@Autowired
public SecurityConfig(@Qualifier("userDetailsServiceImpl") UserDetailsService userDetailsService, Environment env) {
this.userDetailsService = userDetailsService;
this.env = env;
}
//configuration here
@Bean("entityManagerFactory")
@Autowired
public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
Properties properties = new Properties();
properties.put("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
properties.put("hibernate.show_sql", env.getProperty("spring.jpa.show-sql"));
properties.put("current_session_context_class", env.getProperty("spring.jpa.properties.hibernate.current_session_context_class"));
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setPackagesToScan("me.kqlqk.springBootApp.models");
sessionFactory.setHibernateProperties(properties);
return sessionFactory;
}
}
我使用@Bean("entityManagerFactory") 因为没有它我得到了
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in me.kqlqk.springBootApp.service.impl.UserDetailsServiceImpl required a bean named 'entityManagerFactory' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Qualifier(value="userDetailsServiceImpl")
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
和我的 userDetailsServiceImpl class:
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
private UserDAO userDAO;
@Autowired
public UserDetailsServiceImpl(UserDAO userDAO) {
this.userDAO = userDAO;
}
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
User user = userDAO.getByEmail(email);
Set<GrantedAuthority> authorities = new HashSet<>();
user.getRoles().forEach(role -> authorities.add(new SimpleGrantedAuthority(role.getName())));
return new org.springframework.security.core.userdetails.User(
user.getEmail(),
user.getPassword(),
authorities);
}
}
那么,我该如何配置会话工厂或者我的代码哪里有问题?
所以,我只是决定使用@Transactional 而不需要 sessionFactory