关于 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)}

查看错误信息应该是说没有扫描到楼主的 Service,经过一番排查之后,楼主将错误定位到了包扫描的配置上,其实也就是 use-default-filters 配置的问题上。

Step 1:先看一下楼主的 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 的配置文件中,楼主也想当然的配置了 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 进行扫描。楼主稍微总结一下,即 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 就没有错误了!

 

posted @ 2017-12-01 11:02  crawl+  阅读(12433)  评论(5编辑  收藏  举报