0

I updated the my spring-boot project to 2.6.6 from 2.1.6 and I get the following error:

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  securityConfig (field private com.seedC.simplecontab.security.user.service.UserService com.seedC.simplecontab.security.SecurityConfig.userService)
↑     ↓
|  userService defined in file [D:\softDev\SeedC\SimpleContab\pfa-contab-backend\target\classes\com\seedC\simplecontab\security\user\service\UserService.class]
└─────┘

Here are the relevant classes:

@Service
@RequiredArgsConstructor
public class UserService {

    private final UserDTOTransformer userDTOTransformer;
    private final UserRepository userRepository;
    private final PfaRepository pfaRepository;
    private final SettingsRepository settingsRepository;
    private final AuthenticationService authenticationService;
    private final AuthorizationService authorizationService;
    private final PasswordEncoder passwordEncoder;
    private final SubscriptionRepository subscriptionRepository;
    private final SubscriptionPackageService subscriptionPackageService;
    private final EmailValidationService emailValidationService;
    
    **methodes**

}
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    CustomUserDetailsService customUserDetailsService;
    @Autowired
    private JwtAuthenticationEntryPoint unauthorizedHandler;

    @Autowired
    private JwtTokenProvider tokenProvider;
    @Autowired
    private UserService userService;
    @Autowired
    private SubscriptionService subscriptionService;

    **bean definitions and overrides**
}

I don't see how is the UserService dependent on the SecurityConfig. Also, how can this be solved in a safe manner? I could try to use the @Lazy annotation or allow circular dependencies from configuration, but I don't really see why is it necessary in this case.

Thanks!

maybesomename
  • 81
  • 1
  • 9
  • 1
    What beans do you define in SecurityConfig? E.g. if PasswordEncoder etc. are defined there, that's the cycle – dunni Apr 21 '22 at 07:57
  • 1
    Also, for a workaround, check https://stackoverflow.com/questions/70036903/spring-boot-application-fails-to-start-after-upgrading-to-2-6-0-due-to-circular – dunni Apr 21 '22 at 08:01
  • 2
    Your config needs the `UserService` your `UserService` needs the `PaswwordEncoder` which generally comes from the `SecurityConfig`. Hence a circular reference. – M. Deinum Apr 21 '22 at 08:09

1 Answers1

0

As mentioned in multiple comments, if the PasswordEncoder is defined in the SecurityConfig, then it depends on the SecurityConfig. Thus, the solution is to define it in some other place, or restructure my code so that UserService does not depend on PasswordEncoder.

maybesomename
  • 81
  • 1
  • 9