【问题标题】:Field with autowire but get Could not autowire field message具有自动装配的字段但得到无法自动装配字段消息
【发布时间】:2016-02-23 15:01:32
【问题描述】:

在 Spring Boot 应用程序中,我收到此错误

创建名为“webSecurityConfigurerAdapter”的 bean 时出错: 注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:

无法自动装配字段:私有 server.service.UserServiceImpl server.ServerApplicationSecurity.userDetailsS​​ervice;嵌套异常 是org.springframework.beans.factory.NoSuchBeanDefinitionException:

找不到 [server.service.UserServiceImpl] 类型的合格 bean 依赖项:预计至少有 1 个符合自动装配条件的 bean 这种依赖的候选人。依赖注解: {@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;
    }
    ...
}

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

【问题讨论】:

    标签: spring spring-boot spring-4


    【解决方案1】:

    这个:

    @Autowired
    private UserServiceImpl userDetailsService;
    

    应该是这样的:

    @Autowired
    private UserDetailsService userDetailsService;
    

    通过接口而不是实现来引用您的 bean。如果您仍然遇到问题,那么 Spring 可能找不到您的 UserServiceImpl @Service。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2016-03-23
      • 1970-01-01
      • 2014-10-07
      • 2014-09-07
      相关资源
      最近更新 更多