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等这些注解的类,则把这些类 ...
随机推荐
- 《mysql必知必会》学习_第12章_20180801_欢
第12章:汇总数据 P76 avg()函数求平均值 select avg(prod_price) as avg_price from products; #计算prod_price的平均值,并且命名为 ...
- Android中去掉标题栏
在Android中去掉标题栏有三种方法,它们也有各自的特点. 1.在代码里实现 this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 记 ...
- LCA的 RMQ解法模版
struct Edge{ int from, to, nex; }edge[N<<1]; int head[N], edgenum; void addedge(int u, int v){ ...
- 10.Date对象
Date()对象 Date对象用于处理日期和时间. Math对象 ◆Math.ceil() 天花板函数 向上取整 ★如果是整数,取整之后是这个数本身 ★如果是小数,对数进行向上舍入. ◆Ma ...
- linux常用命令(一)linux开关机、重启以及文本界面与图形界面互换
1.开关机 reboot 重启: shutdown [-efFhknr][-t 秒数][时间][警告信息] 关机: shutdown [-efFhknr][-t 秒数][时间][警告信息] 关机: - ...
- ASP.NET MVC5 高级编程-学习日记-第一章 入门
1.1 ASP.NET MVC 简介 ASP.NET是一种构建Web应用程序的框架,它将一般的MVC(Model-View-Controller)模式应用于ASP.NET框架. 1.1.1 MVC模式 ...
- C#复数类的总结
复数是C#中没有的,不能直接调用的.但是我们可以通过封装,构造自己的复数形式.这里我自己封装了一个Complex类,也不知道写得如何.可能还有一些东西没有考虑. 不过这里包含了复数的基本晕算了了,包括 ...
- 真实项目中VS2015中自建T4模板生成文件的使用
有可能许多小伙伴们发现,vs2015和2012的自带T4模板中的.tt文件改变非常之多,如果仅仅copyEF系统自己生成的模板文件,那可累了.以下是我自己整理的在2012和2015中都可以试用的代码. ...
- 【转】C# 之泛型详解
原文地址:https://www.cnblogs.com/yueyue184/p/5032156.html 什么是泛型 我们在编写程序时,经常遇到两个模块的功能非常相似,只是一个是处理int数据,另一 ...
- 【转】[MySQL复制异常]Cannot execute statement: impossible to write to binary log since statement is in row for
MySQL复制错误]Last_Errno: 1666 Last_Error: Error executing row event: 'Cannot execute statement: imposs ...