spring <context:component-scan>使用说明(转)
来源:互联网 发布:unity3d 联网 编辑:程序博客网 时间:2024/05/16 08:36
在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean
注意:如果配置了<context:component-scan>那么<context:annotation-config/>标签就可以不用再xml中配置了,因为前者包含了后者。另外<context:annotation-config/>还提供了两个子标签
1. <context:include-filter>
2. <context:exclude-filter>
在说明这两个子标签前,先说一下<context:component-scan>有一个use-default-filters属性,改属性默认为true,这就意味着会扫描指定包下的全部的标有@Component的类,并注册成bean.也就是@Component的子注解@Service,@Reposity等。所以如果仅仅是在配置文件中这么写
<context:component-scan base-package="tv.huan.weisp.web"/>
Use-default-filter此时为true那么会对base-package包或者子包下的所有的进行java类进行扫描,并把匹配的java类注册成bean。
可以发现这种扫描的粒度有点太大,如果你只想扫描指定包下面的Controller,该怎么办?此时子标签<context:incluce-filter>就起到了勇武之地。如下所示
<context:component-scan base-package="tv.huan.weisp.web .controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
这样就会只扫描base-package指定下的有@Controller下的java类,并注册成bean
但是因为use-dafault-filter在上面并没有指定,默认就为true,所以当把上面的配置改成如下所示的时候,就会产生与你期望相悖的结果(注意base-package包值得变化)
<context:component-scan base-package="tv.huan.weisp.web ">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
此时,spring不仅扫描了@Controller,还扫描了指定包所在的子包service包下注解@Service的java类
此时指定的include-filter没有起到作用,只要把use-default-filter设置成false就可以了。这样就可以避免在base-packeage配置多个包名这种不是很优雅的方法来解决这个问题了。
另外在我参与的项目中可以发现在base-package指定的包中有的子包是不含有注解了,所以不用扫描,此时可以指定<context:exclude-filter>来进行过滤,说明此包不需要被扫描。综合以上说明
Use-dafault-filters=”false”的情况下:<context:exclude-filter>指定的不扫描,<context:include-filter>指定的扫描
源:http://blog.csdn.net/chunqiuwei/article/details/16115135
- spring <context:component-scan>使用说明(转)
- spring <context:component-scan>使用说明(转)
- spring <context:component-scan>使用说明(转)
- spring <context:component-scan>使用说明(转)
- spring <context:component-scan>使用说明(转)
- spring <context:component-scan>使用说明(转)
- spring <context:component-scan>使用说明(转)
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring<context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明。
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- spring <context:component-scan>使用说明
- Windows 64位下安装Redis超详细教程
- EF应用CodeFirst模式,数据迁移的基本用法要点摘记
- PythonBasic
- BZOJ1191(HNOI2006)[超级英雄Hero]--二分图最大匹配
- 05_自定义信号和槽
- spring <context:component-scan>使用说明(转)
- 阿里负载均衡,配置中间证书问题(在starcom申请免费DV ssl)
- Golang构建简单web框架
- msm8916 dt选用规则
- 【C语言】实现一个简单SHELL应用
- C# 把数据字典表的数据自动生成为 js 键值对形式
- python lxml 安装
- QT编译出现错误 “undefined reference to ‘vtable for xxx’”
- BZOJ 1053 反素数【DFS剪枝】