关于context:component-scan配置中use-default-filters参数的作用
参考了多篇文章都说明了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参数的作用的更多相关文章
- Nginx配置中一个不起眼字符"/"的巨大作用
文章转载自:https://mp.weixin.qq.com/s/QwsbuNIqLpxi_FhQ5pSV3w Nginx作为一个轻量级的,高性能的web服务软件,因其占有内存少,并发能力强的特点,而 ...
- 转载ASP.net 中 OutputCache 指令各个参数的作用
使用@ OutputCache指令 使用@ OutputCache指令,能够实现对页面输出缓存的一般性需要.@ OutputCache指令在ASP.NET页或者页中包含的用户控件的头部声明.这种方式非 ...
- ASP.net 中 OutputCache 指令各个参数的作用
使用@ OutputCache指令 使用@ OutputCache指令,能够实现对页面输出缓存的一般性需要.@ OutputCache指令在ASP.NET页或者页中包含的用户控件的头部声明.这种方式非 ...
- ASP.net 中 OutputCache 指令各个参数的作用。
使用@ OutputCache指令 使用@ OutputCache指令,能够实现对页面输出缓存的一般性需要.@ OutputCache指令在ASP.NET页或者页中包含的用户控件的头部声明.这种方式非 ...
- matplotlib中subplot的各参数的作用
subplot(a,b,c)中a代表所画图形的行数 b代表所画图形的列数 c代表所画图形的序号. plt.figure(facecolor='w', figsize=(9, 10)) plt.subp ...
- listview中OnItemClick方法各个参数的作用
OnItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 1.arg0,arg2 m_listview.setOnI ...
- 转 测试linux中expect的timeout参数的作用
http://blog.csdn.net/msdnchina/article/details/50638818
- [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 ...
- Tomcat启动报错org.springframework.web.context.ContextLoaderListener类配置错误——SHH框架
SHH框架工程,Tomcat启动报错org.springframework.web.context.ContextLoaderListener类配置错误 1.查看配置文件web.xml中是否配置.or ...
随机推荐
- combox 同时写入和获取 text ,value
c# combox 同时写入和获取 text ,value 2007-10-10 16:33:44| 分类: c# 知识|举报|字号 订阅 public class ComboBoxItem ...
- wampserver修改mysql默认字符集
[client] default-character-set=utf8 [mysqld]character_set_server = utf8 重启服务
- 长年承接AR图像识别项目,关于高速UnityARCam多图问题技术整理
//关于高通ARCameraQCARBehaviour script下 Max Simultneous Image QCARBehaviour script下 Max Simultneous Imag ...
- 记一次故障处理----主机异常关闭后mongodb二进制文件损坏
今天,在某个演示环境中,我们的产品经历过整个机房断电后,出现了mongodb二进制文件损坏,以下是故障的分析记录过程: 1.在客户处支撑的同事发现整个机房断电再恢复后,3个mongodb复制集中,有1 ...
- MyBatis与Spring整合
1.单独使用MyBatis 单独使用MyBatis,不结合其他框架,主要步骤是: 1.创建SqlSessionFactory对象 创建方法是通过SqlSessionFactoryBuilder这个类从 ...
- 也说php从mysql数据库通过服务器端json返回数据出现乱码问题
我最近需要用js和json与mysql数据库做一个两级联动的下拉菜单,发现当从数据库中返回的是中文时客户端会出现乱码问题,经过在百度上查找终于找到了解决办法如下: while($row=$MySqlc ...
- navicat 连接 oracle
环境:windows2008r2(x64) oracle 11.2.0.1 1.找到Oracle服务端的NetManager程序(一般在开始菜单->oracle->配置和移植工具)中,修改 ...
- about Internet protocol
<1> SSL版本 测试浏览器支持的SSL版本的网站: https://www.ssllabs.com/ssltest/viewMyClient.html 0xfefd (DTLS ...
- Knock: 使用压电传感器来检测敲击
原文链接:https://www.arduino.cc/en/Tutorial/Knock 敲击检测 本教程介绍如何使用压电传感器检测振动,比如敲门.桌子或其他固体表面. 压电传感器是一种能够在振动. ...
- Tornado实战项目(伪JD商城)
预备知识 在之前tornado商城项目中,在开始之前需要引入一些项目设计知识,如接口,抽象方法抽象类,组合,程序设计原则等,个人理解项目的合理设计可增加其灵活性, 降低数据之间的耦合性,提高稳定性,下 ...