我的控制器
@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
服务中同时自动装配引起的。如何防止此错误?
答案 0 :(得分:3)
问题是AuthorizationServiceImpl
的构造函数需要连接UserSearchService
的bean,这意味着UserSearchServiceImpl
将被连线,但UserSearchServiceImpl
的构造函数需要要连接的AuthorizationServiceImpl
的bean(这意味着AuthorizationServiceImpl
将被连接)。这种依赖是循环的,如果没有开发人员的帮助,Spring无法解决它:
AuthorizationServiceImpl
→UserSearchServiceImpl
→AuthorizationServiceImpl
打破这种循环依赖关系的一种方法是将UserSearchService
bean注入AuthorizationServiceImpl
到setter injection,而不是constructor injection。这允许Spring创建AuthorizationServiceImpl
bean而无需创建UserSearchServiceImpl
bean。初始化AuthorizationServiceImpl
后,可以初始化UserSearchServiceImpl
bean。初始化UserSearchServiceImpl
后,可以使用其setter将其连接到AuthorizationServiceImpl
。这减少了对以下内容的依赖性:
UserSearchServiceImpl
→AuthorizationServiceImpl
为此,请将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;
}
...
}
请务必注意,该技术要求userSearchService
和authorizationService
字段可变(删除final
限定符)。