這篇文章主要介紹了Idea 解決 Could not autowire. No beans of 'xxxx' type found 的錯誤提示,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
IntelliJ Idea 解決 Could not autowire. No beans of ‘xxxx' type found 的錯誤提示
哈,在使用 @Autowired 時,今天又遇一坑,這倆波浪線是幹啥子嘛:
然鵝,試了一下,控制檯也不報錯,可以正常運行,
數據也有:
於是,又再百度上找答案。。
問題分析
在 Idea 的 spring 工程裏,經常會遇到 Could not autowire. No beans of ‘xxxx' type found 的錯誤提示。但程序的編譯和運行都是沒有問題的,這個錯誤提示並不會產生影響。但紅色的錯誤提示在有些有強迫症的程序員眼裏,多多少少有些不太舒服。
問題原因其一
第一個是 Intellij IDEA 本身工具的問題。
解決辦法:
(1)不理它。
(2)在註解上加上:
@Autowired(required = false)
(3)降低 Autowired 檢測的級別,將 Severity 的級別由之前的 error 改成 warning 或其它可以忽略的級別。
還有一個原因
這個博主沒有遇到,友情粘貼!
第二個便是我們導入 @Service 包的時候導入包錯誤造成的。
spring auto scan 配置,在編輯情況下,無法找不到對應的bean,於是提示找不到對應 bean 的錯誤。常見於 mybatis 的 mapper,如下:
<!-- mapper scanner configurer --> <bean id="mapperScannerConfig" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.adu.spring_test.mybatis.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>
解決辦法:
錯誤導包 import com.alibaba.dubbo.config.annotation.Service;
正確的包應該是下面這個:
import org.springframework.stereotype.Service;
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。