Spring <context:component-scan>标签属性 use-default-filters 以及子标签 include-filter使用说明
Spring <context:component-scan>标签作用有很多,最基本就是 开启包扫描,可以使用@Component、@Service、@Component等注解;
今天要作为发现,记录该标签的属性 use-default-filters 以及子标签 include-filter使用方式 ;
use-default-filters 默认true,默认会扫描@Component、@Controller、@Service、@Repository注解,因为这些注解都可以说是@Component注解的,后面三个注解相当于是@Component注解的子类;
<xsd:attribute name="use-default-filters" type="xsd:boolean"
default="true">
<xsd:annotation>
<xsd:documentation><![CDATA[
Indicates whether automatic detection of classes annotated with @Component, @Repository, @Service,
or @Controller should be enabled. Default is "true".
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
通常SpringWeb项目使用Spring、SpringMvc,那就会存在两个Spring容器,父子关系的容器;我们一般在SpringWeb的配置文件里开启注解扫描,我们只希望扫描Controller形式的注解;
假如存在如下包结构:
com.xxx
|
|-----controller
|
|-----service
比如我会想我只扫描Controller注解的类,
<context:component-scan base-package="com.xxx">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
一般来看,可能觉得没有什么问题,@Controller也扫描加入Spring容器了;但是仔细看会发现@Service、@Component、@Repository等标注的类也扫描出来的;
将use-default-filters设置为 false
<context:component-scan base-package="com.xxx" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
再测试发现,果然只扫描了@Controller的注解;原因呢:
use-default-filters="false"的时候,在设置<context:include-filter/> 相当于加白名单 ,我只会扫描你允许扫描的注解,就像QQ好友聊天设置了只和好友聊天,不是好友的不能发消息给我;
或者 use-default-filters="true"的时候,在设置<context:exclude-filter/> 相当于加黑名单 ,(上面四种注解@Component、@Service、@Controller、@Repository我都会扫描,至于哪些不要了你说了算,打个比方,QQ上的黑名单,你可以和好友列表中好友聊天,但是不能和黑名单里的发消息吧,差不多这个意思,反正我好久没用过黑名单了。。。
当然了,也可以设置扫描包级别细分到com.xxx.controller,严格按照包定义来扫描,也不会任何问题;
包扫描不严格会导致的问题,Spring事务失效的问题,甚至还会有其他各种问题出现,比如Spring容器、SpringMvc容器都扫描到Service层,这时候父子容器都有Service的bean了,默认先在本容器查找,再去父亲容器查找,两个bean能一样嘛?一个AOP动态代理后的,一个原生态的bean,这时候就会出现事务失效问题,后面会模拟记录下这个问题的;
Spring <context:component-scan>标签属性 use-default-filters 以及子标签 include-filter使用说明的更多相关文章
- 自定义标签TLD文件中,rtexprvalue子标签的意思
rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否能够利用JSP表达式. 举例子: 1.定义一个TLD文件: <tag> <name ...
- Spring基础07——配置集合属性
1.集合属性 在Spring中可以通过一组内置的xml标签(例如<list>,<set>或<map>)来配置集合属性. 2.配置List集合 配置java.util ...
- jQuery 获取标签属性值的问题
jquery attr()无法获取属性值问题 css里明明已经设置过了: 可还是获取不了: 求指导. 一定是undefined,attr是用来获得或设置标签属性的,不是用来获得CSS属性的.如果你 ...
- DOM标签属性和对象属性
DOM元素的属性分为两种 (1)标签属性 直接写在标签上的属性 (2)对象属性 由于所有的DOM元素都是Object类型,所以我们可以通过对象的方式为DOM元素设置属性 1.标签属性 (1)设置标签属 ...
- JSTL标签,EL表达式,OGNL表达式,struts2标签 汇总
一下纯属个人总结摘抄,总结一起方便查看,解决疑问,有遗漏或错误,还请指出. 1,JSTL标签总结: a).JSTL标签有什么用? JSTL是由JCP(Java Commu ...
- JSTL标签库的基本教程之核心标签库(二)
JSTL标签库的基本教程之核心标签库(二) 核心标签库 标签 描述 <c:out> 用于在JSP中显示数据,就像<%= ... > <c:set> 用于保存数据 & ...
- [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 ...
- Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别
转: http://blog.csdn.net/it_man/article/details/5074371 Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之 ...
- spring <context:component-scan>使用说明(转)
在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类 ...
随机推荐
- SystemProperties.get/set property_get/set
在java层设置系统属性要注意几点: 1 需要有系统权限.可以在AndroidManifest.xml添加android:sharedUserId="android.uid.system&q ...
- python3使用ip地址代理
第一种IP地址代理方式from urllib import request if __name__ == "__main__": # 访问网址 url = 'http://www. ...
- 关于synchronized和lock 的使用及其在线程间的通信
题目要求:子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程又循环100,如此循环50次 synchronized的使用 import java.util.conc ...
- dot net core 使用 IPC 进程通信
本文告诉大家如何使用dot net core 和其他进程进行通信 一般都是使用 WCF 或 remoting 做远程通信,但是 dot net core 不支持 WCF 所以暂时我就只能使用 管道通信 ...
- 如何获取SQL中Print语句输出内容
SqlConnection cn = new SqlConnection("server=my\\my2005;database=rdwhdata2005;user id=zjh;passw ...
- 背水一战 Windows 10 (57) - 控件(集合类): ListViewBase - 增量加载, 分步绘制
[源码下载] 背水一战 Windows 10 (57) - 控件(集合类): ListViewBase - 增量加载, 分步绘制 作者:webabcd 介绍背水一战 Windows 10 之 控件(集 ...
- ConcurrentHashMap源码解析(3)
此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 4.get(Object key) 使用方法: map.get("hello"); 源代 ...
- SpringCloud实现集群和负载均衡
Spring cloud是一个基于Spring Boot实现的服务治理工具包,在微服务架构中用于管理和协调服务的. 组成部分 spingcloud的五大神兽 服务发现——Netflix Eureka ...
- SQL 将一列多行数据合并为一行
原表数据: 期望结果: 使用STUFF + FOR XML PATH即可实现以上效果 执行以下SQL: , , '') AS Course FROM Student AS T 可以看到输出结果与期望结 ...
- django基础之一
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...