IntelliJ Idea 解決 Could not autowire. No beans of 'xxxx' type found 的錯誤提示
阿新 • • 發佈:2019-01-10
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;