關於 use-default-filters 屬性的說明,

來源:互聯網
上載者:User

關於 use-default-filters 屬性的說明,

原創播客,如需轉載請註明出處。原文地址:http://www.cnblogs.com/crawl/p/7940755.html 

----------------------------------------------------------------------------------------------------------------------------------------------------------

筆記中提供了必要的程式碼範例,需要說明的是,大部分程式碼範例都是本人所敲代碼並進行測試,不足之處,請大家指正~

本部落格中所有言論僅代表博主本人觀點,若有疑惑或者需要本系列分享中的資料工具,敬請聯絡 qingqing_crawl@163.com

-----------------------------------------------------------------------------------------------------------------------------------------------------------

前言:昨天在做一個小項目時先對 SSM 進行整合,一通配置之後進行測試,發現啟動 Tomcat 就出錯了,錯誤資訊如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mybatis.ssm.service.EmployeeService com.mybatis.ssm.controller.EmployeeController.employeeService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.mybatis.ssm.service.EmployeeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mybatis.ssm.service.EmployeeService com.mybatis.ssm.controller.EmployeeController.employeeService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.mybatis.ssm.service.EmployeeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

查看錯誤資訊應該是說沒有掃描到 LZ 的 Service,經過一番排查之後,LZ 將錯誤定位到了包掃描的配置上,其實也就是 use-default-filters 配置的問題上。

Step 1:先看一下LZ 的 SpringMVC 和 Spring 的設定檔中對包掃描的配置:

SpringMVC:

1 <!-- 配置自動掃描的包 -->2     <context:component-scan base-package="com.mybatis.ssm" use-default-filters="false">3         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>4     </context:component-scan>

Spring:

1 <!-- 配置自動掃描的包 -->2     <context:component-scan base-package="com.mybatis.ssm" use-default-filters="false">3         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>4     </context:component-scan>

SpringMVC 主要就是來管理網站的跳轉邏輯,所以在配置掃描的包時,使用 use-default-filters 屬性,並設定為 false,即不使用預設的 Filter 進行掃描。

而 Spring 的設定檔中,LZ 也想當然的配置了 use-default-filters 為 false,正式這個配置,導致出錯。

Step 2:錯誤分析

要分析這個錯誤,就要先瞭解 use-default-filters 這個屬性的作用。use-default-filters 屬性的預設值為 true,即使用預設的 Filter 進行包掃描,而預設的 Filter 對標有 @Service,@Controller和@Repository 的註解的類進行掃描,因為前面說過,我們希望 SpringMVC 只來控制網站的跳轉邏輯,所以我們只希望 SpringMVC 的配置掃描 @Controllerce 註解標註的類,不希望它掃描其餘註解標註的類,所以設定了 use-default-filters 為 false,並使用 context:include-filter 子標籤設定其只掃描帶有 @Controller 註解標註的類。

而 Spring 就不同了,我們希望 Spring 只不掃描帶有 @Controller 註解標註的類,而掃描其他註解標註的類,而這時建立在使用預設的 Filter 進行掃描的基礎上,設定了 context:exclude-filter 標籤,不掃描 @Controller 註解標註的類,所以不應該設定 use-default-filters 為 false,所以這就解釋了為什麼一開始啟動 Tomcat 時報了一個找不到 Service 的錯誤。

Step 3:總結

在使用 use-default-filters 屬性時要分清楚需要掃描哪些包,是不是需要使用預設的 Filter 進行掃描。LZ 稍微總結一下,即 use-default-filters="false" 需要和 context:include-filter 一起使用,而不能和 context:exclude-filter 屬性一起使用。

Step 4:更正

Spring 的設定檔中包掃描配置如下:

1 <!-- 配置自動掃描的包 -->2     <context:component-scan base-package="com.mybatis.ssm">3         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>4     </context:component-scan>

這樣更正過來之後,再啟動 Tomcat 就沒有錯誤了!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.