解決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中的使用


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM