Application Failed to start 部分Bean的Dependencies形成了一个循环... 为什么?

Application Failed to start The Dependencies of some of the Beans form a cycle... Why?

所以我在AppConfig.java中有这段代码:

@Configuration
@EnableWebSecurity
public class AppConfig extends WebSecurityConfigurerAdapter {

    private final CurrentUserService currentUserService;
    private final SessionFilter sessionFilter;
    private final PasswordEncoder passwordEncoder;

    @Autowired
    @Lazy
    public AppConfig(CurrentUserService currentUserService, SessionFilter sessionFilter, PasswordEncoder passwordEncoder) {
        this.currentUserService = currentUserService;
        this.sessionFilter = sessionFilter;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(currentUserService).passwordEncoder(passwordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http = http.cors().and().csrf().disable();

        http = http.exceptionHandling().authenticationEntryPoint(
                (request, response, authException) -> {
                    response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
                }
        ).and();

        http.authorizeRequests().antMatchers("/api/login").permitAll().anyRequest().authenticated();

        http.addFilterBefore(sessionFilter, UsernamePasswordAuthenticationFilter.class);
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

如果我去掉 @Lazy 它将不会启动,我试图去掉构造函数并执行:

    @Autowired
    private final CurrentUserService currentUserService;
    @Autowired
    private final SessionFilter sessionFilter;
    @Autowired
    private final PasswordEncoder passwordEncoder;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(currentUserService).passwordEncoder(passwordEncoder);
    }

同样的事情,有人能帮帮我吗,我真的不想使用@Lazy 实现。这是错误 returns:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appConfig' defined in file [C:\Users\Blade\Documents\WebMent\modules\backend\build\classes\java\main\com\shortsdesigns\webment\configuration\AppConfig.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'appConfig': Requested bean is currently in creation: Is there an unresolvable circular reference?

当摆脱构造函数并使用字段注入时,它给我这个错误:

Error creating bean with name 'appConfig': Unsatisfied dependency expressed through field 'passwordEncoder'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'appConfig': Requested bean is currently in creation: Is there an unresolvable circular reference?

发生此错误是因为您在注入它的 class 中创建了 PasswordEncoder

最好的解决方案是根本不自动装配 PasswordEncoder(或 CurrentUserService)。

这些实例似乎只用在 configure(AuthenticationManagerBuilder auth) 方法中,这是多余的。

PasswordEncoderUserDetailsService 注册为 bean 足以让 Spring 安全检测到它们并在您的配置中使用它们。

换句话说,您应该删除以下代码:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(currentUserService).passwordEncoder(passwordEncoder);
}

应用程序仍将以完全相同的方式运行。