1. 程式人生 > >IntelliJ Idea解決Could not autowire. No beans of 'xxxx' type found的錯誤提示

IntelliJ Idea解決Could not autowire. No beans of 'xxxx' type found的錯誤提示

images clas sql figure war configure 多少 錯誤提示 bsp

本文轉自:http://blog.csdn.net/u012453843/article/details/54906905

1.問題描述

  在Idea的spring工程裏,經常會遇到Could not autowire. No beans of ‘xxxx‘ type found的錯誤提示。但程序的編譯和運行都是沒有問題的,這個錯誤提示並不會產生影響。但紅色的錯誤提示在有些有強迫癥的程序員眼裏,多多少少有些不太舒服。

技術分享

2. 原因

原因可能有兩個,第一個是IntellijIDEA本身工具的問題。第二個便是我們導入@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>

3. 解決方案

  針對第一種原因,解決辦法是:降低Autowired檢測的級別,將Severity的級別由之前的error改成warning或其它可以忽略的級別。

技術分享

針對第二種原因,解決方案當然是導入正確的包。首先我們來看下最容易導入的錯誤包,如下所示:

importcom.alibaba.dubbo.config.annotation.Service;
   正確的包應該是下面這個
importorg.springframework.stereotype.Service;
切記切記啊!!!!

IntelliJ Idea解決Could not autowire. No beans of 'xxxx' type found的錯誤提示