在Spring启动应用程序中,我收到此错误
使用名称' webSecurityConfigurerAdapter'创建bean时出错: 注入自动连接的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:
无法自动装配字段:private server.service.UserServiceImpl server.ServerApplicationSecurity.userDetailsService;嵌套异常 是org.springframework.beans.factory.NoSuchBeanDefinitionException:
找不到类型为[server.service.UserServiceImpl]的限定bean 依赖:预计至少有1个bean有资格成为autowire 这种依赖的候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}
@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;
}
...
}
我的班级使用@Service并且bean已经自动装配,所以我不明白为什么会出现这个错误。
答案 0 :(得分:1)
此:
@Autowired
private UserServiceImpl userDetailsService;
应该是这样的:
@Autowired
private UserDetailsService userDetailsService;
通过接口引用bean,而不是通过实现引用。如果您仍然遇到问题,那么Spring可能无法找到您的UserServiceImpl @Service。