问题
@Autowired注入一个接口, 一个接口下有多个实现类时, 自动注入接口造成idea检测到错误
Could not autowire. There is more than one bean of 'UserDetailsService' type.
![](https://img2020.cnblogs.com/blog/1704051/202007/1704051-20200722123322092-432106685.png)
图1 问题
原因
一个接口下有多个实现类, idea不知道该注入哪个类实现这个接口, 必须指定实现类的具体名字
解决
在字段上使用@Qualifier搭配@Autowired进行构造方法注入
首先, 为service层的实现类指定名字
![](https://img2020.cnblogs.com/blog/1704051/202007/1704051-20200722123838906-1501373807.png)
图2 为@Service指定名字
使用构造器注入, 在构造参数的字段上使用Qualifier注解
![](https://img2020.cnblogs.com/blog/1704051/202007/1704051-20200722124135189-1286314676.png)
图2 使用@Qualifier注解