问题

@Autowired注入一个接口, 一个接口下有多个实现类时, 自动注入接口造成idea检测到错误
Could not autowire. There is more than one bean of 'UserDetailsService' type.


Could not autowire
图1 问题

原因

一个接口下有多个实现类, idea不知道该注入哪个类实现这个接口, 必须指定实现类的具体名字

解决

在字段上使用@Qualifier搭配@Autowired进行构造方法注入

首先, 为service层的实现类指定名字


Could not autowire
图2 为@Service指定名字

使用构造器注入, 在构造参数的字段上使用Qualifier注解


Could not autowire
图2 使用@Qualifier注解

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
  • 2021-11-22
  • 2021-04-14
  • 2021-11-28
猜你喜欢
  • 2021-09-28
  • 2021-06-23
  • 2021-03-30
  • 2021-06-24
  • 2021-06-30
相关资源
相似解决方案