参考了多篇文章都说明了use-default-filters参数的基本用途,但有些主要点没有说到,这里补充记录下:

<context:component-scan base-package="com.jaamy"  use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

这个只扫描com.jaamy包下的@Controller,不会扫描@Service、@Repository

<context:component-scan base-package="com.jaamy">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

这个不但扫描com.jaamy包下的@Controller,同时也会扫描@Service、@Repository,注意这里没有添加use-default-filters参数

下面配合源码说明下use-default-filters参数的作用以及和context:include-filter、exclude-filter的关系。

代码中是根据use-default-filters的值来确定是否需要调用registerDefaultFilters来添加默认的filters到includeFilters中,看下面的代码:

org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider

public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment) {
if (useDefaultFilters) {
registerDefaultFilters();
}
Assert.notNull(environment, "Environment must not be null");
this.environment = environment;
}

use-default-filters为true时调用了下面的代码,在includeFilters列表中添加了Component、ManagedBean和Named,因此use-default-filters的值直接影响includeFilters的内容,而includeFilters的容直接影响了要扫描的内容,因此use-default-filters的值是否配置也就决定了整体要扫描的内容。

protected void registerDefaultFilters() {
this.includeFilters.add(new AnnotationTypeFilter(Component.class));
ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
try {
this.includeFilters.add(new AnnotationTypeFilter(
((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
logger.debug("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
}
catch (ClassNotFoundException ex) {
// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
}
try {
this.includeFilters.add(new AnnotationTypeFilter(
((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
}
catch (ClassNotFoundException ex) {
// JSR-330 API not available - simply skip.
}
}

关于include-filter、exclude-filter的作用主要是用来过滤扫描到的bean是否合法:

首先通过exclude-filter进行黑名单过滤;

然后通过include-filter进行白名单过滤;

否则默认排除。 看下面的代码:

protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
for (TypeFilter tf : this.excludeFilters) {
if (tf.match(metadataReader, this.metadataReaderFactory)) {
return false;
}
}
for (TypeFilter tf : this.includeFilters) {
if (tf.match(metadataReader, this.metadataReaderFactory)) {
return isConditionMatch(metadataReader);
}
}
return false;
}

总结:

<context:component-scan base-package="com.jaamy"  use-default-filters="false">
         <context:include-filter type="annotation"  expression="org.springframework.stereotype.Controller" />
</context:component-scan>

针对上面的配置,use-default-filters的作用如下:

use-default-filters不配置或者是配置为true时:

不但要扫描include配置的com.jaamy包下的@Controller,而且还要扫描@Service和@Repository

use-default-filters配置为false时:

只扫描include配置的com.jaamy包下的@Controller,不扫描@Service和@Repository

参考地址:http://jinnianshilongnian.iteye.com/blog/1762632

关于context:component-scan配置中use-default-filters参数的作用的更多相关文章

  1. Nginx配置中一个不起眼字符"/"的巨大作用

    文章转载自:https://mp.weixin.qq.com/s/QwsbuNIqLpxi_FhQ5pSV3w Nginx作为一个轻量级的,高性能的web服务软件,因其占有内存少,并发能力强的特点,而 ...

  2. 转载ASP.net 中 OutputCache 指令各个参数的作用

    使用@ OutputCache指令 使用@ OutputCache指令,能够实现对页面输出缓存的一般性需要.@ OutputCache指令在ASP.NET页或者页中包含的用户控件的头部声明.这种方式非 ...

  3. ASP.net 中 OutputCache 指令各个参数的作用

    使用@ OutputCache指令 使用@ OutputCache指令,能够实现对页面输出缓存的一般性需要.@ OutputCache指令在ASP.NET页或者页中包含的用户控件的头部声明.这种方式非 ...

  4. ASP.net 中 OutputCache 指令各个参数的作用。

    使用@ OutputCache指令 使用@ OutputCache指令,能够实现对页面输出缓存的一般性需要.@ OutputCache指令在ASP.NET页或者页中包含的用户控件的头部声明.这种方式非 ...

  5. matplotlib中subplot的各参数的作用

    subplot(a,b,c)中a代表所画图形的行数 b代表所画图形的列数 c代表所画图形的序号. plt.figure(facecolor='w', figsize=(9, 10)) plt.subp ...

  6. listview中OnItemClick方法各个参数的作用

    OnItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 1.arg0,arg2 m_listview.setOnI ...

  7. 转 测试linux中expect的timeout参数的作用

    http://blog.csdn.net/msdnchina/article/details/50638818

  8. [Spring Boot] Use Component Scan to scan for Bean

    Component Scan is important concept when we want to create Bean. Currently we know what, for the cla ...

  9. Tomcat启动报错org.springframework.web.context.ContextLoaderListener类配置错误——SHH框架

    SHH框架工程,Tomcat启动报错org.springframework.web.context.ContextLoaderListener类配置错误 1.查看配置文件web.xml中是否配置.or ...

随机推荐

  1. 彻底卸载 RAD Studio 2009/2010/XE

    彻底卸载 RAD Studio 2009/2010/XE: 控制面板-->添加/删除程序中执行了卸载操作以后, 还需要做以下工作: 1. 删除以下目录(注意有些目录是隐藏的): %ALLUSER ...

  2. Android学习笔记(十)

    Android控件——ListView ListView允许用户通过手指上下滑动的方式将屏幕外的数据滚动到屏幕内,同时 屏幕上原有的数据则会滚动出屏幕. 1.ListView的简单用法 新建一个Lis ...

  3. NSOperationQueue与GCD的使用原则和场景

    首先,我们要明确NSOperationQueue与GCD之间的关系: NSOpertaionQueue用GCD构建封装的,是GCD的高级抽象. 其次,我们要区别两者的不同: GCD仅仅支持FIFO队列 ...

  4. 一个人独立开发 3D 游戏引擎可能吗?

    作者:孙志超链接:https://www.zhihu.com/question/24733255/answer/42000966来源:知乎著作权归作者所有,转载请联系作者获得授权. 当然可以,但难道有 ...

  5. AMap行政区查询服务

    AMap.DistrictSearch行政区查询服务插件,提供全国各省.市.县.区的中心点经纬度.行政区边界坐标组.下级行政区等信息.根据行政区边界坐标组可在地图上绘制行政区边界.(本文为原创,并在项 ...

  6. 同时闪烁多个要素代码(ArcEngine)

    /// <summary> /// 根据查询条件构造/// </summary> /// <param name="where">查询条件< ...

  7. poj 1806 Manhattan 2025

    点击打开链接 题目大意就是给定一个最大歩数,让你输出你在三维的空间中可以到达的位置的切片,注意当歩数大于9的时候就不需要输出了! #include<stdio.h> #include< ...

  8. [DFNews] Blackbag发布MacQuisition 2013 R2

    New in MacQuisition 2013 R2: Improved FileVault 2 Detection - Automatically detect the presence of a ...

  9. 【解决】若要使用报表生成器,必须在此计算机上安装 .Net Framework 3.5

    在报表库中试图通过点击菜单“在报表生成器中编辑”编辑报表时,会跳转到错误信息页面,提示: 若要使用报表生成器,必须在此计算机上安装 .Net Framework 3.5. 一般情况下并不是因为没有安装 ...

  10. javascript_this的用法

    javascript : this的用法 1.this代表全局对象 2.作为函数对象的公共方法(new对象后,可以调用带this关键字的属性) 总结:如果在javascript语言里没有通过new(包 ...