请求的bean目前正在创建中

时间:2018-04-05 00:27:30

标签: java spring

我的控制器

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

初始化bean服务

@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;
    }
...
}

初始化另一个服务bean

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

编译期间抛出以下错误:

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?

错误是由UserSearchService服务在控制器和AuthorizationService服务中同时自动装配引起的。如何防止此错误?

1 个答案:

答案 0 :(得分:3)

问题是AuthorizationServiceImpl的构造函数需要连接UserSearchService的bean,这意味着UserSearchServiceImpl将被连线,但UserSearchServiceImpl的构造函数需要要连接的AuthorizationServiceImpl的bean(这意味着AuthorizationServiceImpl将被连接)。这种依赖是循环的,如果没有开发人员的帮助,Spring无法解决它:

AuthorizationServiceImplUserSearchServiceImplAuthorizationServiceImpl

打破这种循环依赖关系的一种方法是将UserSearchService bean注入AuthorizationServiceImplsetter injection,而不是constructor injection。这允许Spring创建AuthorizationServiceImpl bean而无需创建UserSearchServiceImpl bean。初始化AuthorizationServiceImpl后,可以初始化UserSearchServiceImpl bean。初始化UserSearchServiceImpl后,可以使用其setter将其连接到AuthorizationServiceImpl。这减少了对以下内容的依赖性:

UserSearchServiceImplAuthorizationServiceImpl

为此,请将AuthorizationServiceImpl更改为以下内容:

@Service("authorizationService")
public class AuthorizationServiceImpl implements AuthorizationService {
    private UserSearchService userSearchService;

    public AuthorizationServiceImpl() {}

    @Autowired
    public void setUserSearchService(UserSearchService userSearchService) {
        this.userSearchService = userSearchService;
    }

    ...

}

UserSearchServiceImpl以下内容:

@Service("userSearchService")
public class UserSearchServiceImpl implements UserSearchService {

    private final UserRepository userRepository;
    private AuthorizationService authorizationService;

    @Autowired
    public UserSearchServiceImpl(@NotNull final UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Autowired
    public void setAuthorizationService(AuthorizationService authorizationService) {
        this.authorizationService = authorizationService;
    }

    ...
}

请务必注意,该技术要求userSearchServiceauthorizationService字段可变(删除final限定符)。