回圈依賴錯誤。由于我是這項技術的新手,我無法解決這個問題。我需要一些來解決這個問題。此代碼是為了保護現有專案中的幾個頁面。我只需要保護 3 個頁面,只能由管理員訪問。任何可以擺脫回圈依賴或可以完成我的任務的解決方案都會有所幫助。我的任務是完成安全的幾個頁面訪問用戶。此代碼取自 stackoverflow 片段。
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/editincident","editaccident","editreqeust").authenticated()
.anyRequest().permitAll()
.and()
.csrf().disable();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
}

uj5u.com熱心網友回復:
在 spring.io 檔案中有“如果您主要使用建構式注入,則可能會創建無法決議的回圈依賴場景。
例如:A類通過建構式注入需要B類的實體,B類通過建構式注入需要A類的實體。如果為類 A 和 B 配置 bean 以相互注入,Spring IoC 容器會在運行時檢測到此回圈參考,并拋出 BeanCurrentlyInCreationException。
一種可能的解決方案是編輯某些類的源代碼以由設定器而不是構造器配置。或者,避免建構式注入并僅使用 setter 注入。也就是說,雖然不推薦,但是可以通過setter注入來配置回圈依賴。”
因此,您應該使用建構式方法更改創建 bean 之一的方式
uj5u.com熱心網友回復:
您可以在檔案中寫一行application.properties來消除此錯誤。
spring.main.allow-circular-references= true
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/463985.html
