Requested bean is currently in creation

0

Mam kontroler

@RestController
public class CheckUserDataRestController {
    private final UserSearchService userSearchService;
    @Autowired
    public CheckUserDataRestController(final UserSearchService userSearchService) {
        this.userSearchService = userSearchService;
    }
...
}

który autowired bean serwis

@Service("userSearchService")
public class UserSearchServiceImpl implements UserSearchService {
    private final UserRepository userRepository;
    private final AuthorizationService authorizationService;
    @Autowired
    public UserSearchServiceImpl(
            @NotNull final UserRepository userRepository,
            @NotNull final AuthorizationService authorizationService
    ) {
        this.userRepository = userRepository;
        this.authorizationService = authorizationService;
    }
...
}

który natomiast autowired bean serwis

@Service("authorizationService")
public class AuthorizationServiceImpl implements AuthorizationService {
    private final UserSearchService userSearchService;
    @Autowired
    public AuthorizationServiceImpl(final UserSearchService userSearchService) {
        this.userSearchService = userSearchService;
    }
...
}

Podczas kompilacji wyrzuca

2018-04-05 02:09:39.024  WARN 1864 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'checkUserDataRestController' defined in file [C:\Users\Jonatan\Documents\GitHub\REST-Web-Services\web\out\production\classes\com\web\web\controller\CheckUserDataRestController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'userSearchService' defined in file [C:\Users\Jonatan\Documents\GitHub\REST-Web-Services\core\out\production\classes\com\core\jpa\service\UserSearchServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'authorizationService' defined in file [C:\Users\Jonatan\Documents\GitHub\REST-Web-Services\core\out\production\classes\com\core\service\impl\AuthorizationServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: 
Error creating bean with name 'userSearchService': Requested bean is currently in creation: Is there an unresolvable circular reference?

Błąd jest spowodowany tym, że usługa UserSearchService jest automatycznie uwierzytelniana(autowired) w kontrolerze i usłudze AuthorizationService. Jak zapobiec temu błędowi? W kontrolerze parametr 0 konstruktora prowadzi do UserSearchService , w tym serwisie parametr 1 konstruktora prowadzi do AuthorizationService. Natomiast w tym serwisie parametr 0 konstruktora prowadzi znowu do UserSearchService . Czyli UserSearchService jest @Autowired w kontrolerze i serwisie AuthorizationService. Dlatego powoduje błąd, ale nie wiem dlaczego tak się dzieje.

0

screenshot-20180405073945.png
Dokumentacja sugeruje, aby jeden z Twoich beanów miał możliwość wstrzykiwania przez setter

3

Albo wydzielić klasę z UserSearchService, która nie będzie miała zależności do AuthorizationService. Cykliczne zależności to syf.

1 użytkowników online, w tym zalogowanych: 0, gości: 1