Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Requested bean is currently in creation: Is there an unresolvable circular reference? while using PasswordEncoder

@Configuration
@EnableWebSecurity
@AllArgsConstructor
public class SecurityConfig  {


    private final UserDetailsService userDetailsService;



    @Bean
    public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
        return httpSecurity
                .csrf(csrf -> csrf.disable())
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers("/api/auth/**").permitAll()
                        .anyRequest().authenticated()
                )
                .build();
    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }
    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
            throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }
}

while running above code i got error Requested bean is currently in creation: Is there an unresolvable circular reference? using PasswordEncoder Bean why it i got this error how to solve this error.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The circular dependency occurs as follows:

  1. The SecurityConfig class has a dependency on the UserDetailsService bean, which is likely provided by Spring Security.
  2. The UserDetailsService bean requires the PasswordEncoder bean to encode and decode passwords for authentication.
  3. The PasswordEncoder bean, in turn, depends on the SecurityConfig class to create the AuthenticationManager.

This creates a cycle in the dependency graph, leading to the circular reference error.

To solve this error, you can break the circular dependency by making some changes to the code:

  1. Move the PasswordEncoder bean creation to a separate @Configuration class. This way, it won’t be involved in the circular reference.

    @Configuration
    public class PasswordEncoderConfig {
    @Bean
    PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
    }
    }

  2. Remove the @Autowired annotation from the configureGlobal method in the SecurityConfig class. Spring will automatically inject the UserDetailsService into the AuthenticationManagerBuilder without the need for explicit @Autowired.

    @Configuration
    @EnableWebSecurity
    @AllArgsConstructor
    public class SecurityConfig {

     private final UserDetailsService userDetailsService;
    
     // ... (other methods)
    
     public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
         authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
     }
    
     // ... (other methods)
    

    }

By separating the PasswordEncoder bean into its configuration class and removing the @Autowired from the configureGlobal method, you should be able to resolve the circular dependency issue and avoid the "Requested bean is currently in creation" error. Remember to check other parts of your codebase to ensure there are no other circular dependencies that could lead to similar issues.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading