带有自动装配的字段但得到 Could not autowire field message

Field with autowire but get Could not autowire field message

在 spring 引导应用程序中,我收到此错误

Error creating bean with name 'webSecurityConfigurerAdapter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:

Could not autowire field: private server.service.UserServiceImpl server.ServerApplicationSecurity.userDetailsService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:

No qualifying bean of type [server.service.UserServiceImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class ServerApplicationSecurity extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserServiceImpl userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }
    ...
}

@Service
public class UserServiceImpl implements UserDetailsService, UserService {

    private final UserAppRepository repository;

    @Autowired
    public UserServiceImpl(final UserAppRepository repository) {
        this.repository = repository;
    }
    ...
}

我的 class 使用 @Service 并且 bean 已自动装配,所以我不明白为什么会出现此错误。

这个:

@Autowired
private UserServiceImpl userDetailsService;

应该是这样的:

@Autowired
private UserDetailsService userDetailsService;

通过接口引用您的 bean,而不是通过实现。如果您仍然遇到问题,那么 Spring.

可能找不到您的 UserServiceImpl @Service