Could not autowire

问题

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


图1 问题

原因

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

解决

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

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


图2 为@Service指定名字

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


图2 使用@Qualifier注解

posted @ 2020-07-22 12:43  FuKai  阅读(1498)  评论(0编辑  收藏  举报