解决IDEA报错Could not autowire. There is more than one bean of 'xxx' type

更新项目之后IDEA突然出现了这样的报错信息。显示Could not autowire. There is more than one bean of 'xxx' type。这个错误的意思是xxx类型有不止一个bean,但是这个错误不会影响项目运行,相当于一个warning。


导致这个错误的原因通常是注入的类型有其他的实现类,所以IDEA提示注入的时候会冲突。比如我的项目出现这个错误的原因是项目中新增了一个定制的插件,这个插件里重写了这个类。
因此出现这个问题的时候可以有两种办法解决。
1.给不同的实现标注名字
使用Qulifier注解标注

@Autowired
@Qualifier(name = 'testService1')
private TestService testService;

2.使用@Primary

@Component
@Primary
public class TestService{}

参考文章

  1. StackOverFlow: Could not autowire, there is more than one bean error

  2. CSDN: spring @Primary-在spring中的使用

原文地址:https://www.cnblogs.com/rever/p/11250396.html