我是 Spring 框架的新手。我有一個 Spring Boot 應用程式(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.
我的配置類:
@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.
和我的 userDetailsS??erviceImpl 類:
@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);
}
}
那么,如何配置會話工廠或代碼中的問題在哪里?
uj5u.com熱心網友回復:
所以,我只是決定使用 @Transactional 并且不需要 sessionFactory
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/473498.html
