spring <context:component-scan>使用说明
来源:互联网 发布:三菱plc编程手册fx1n 编辑:程序博客网 时间:2024/05/04 23:50
注意:如果配置了<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>指定的不扫描(对于exculude-filter不存在包不精确后都进行扫描的问题。Use-dafault-filters=”false,可以不用写),<context:include-filter>指定的扫描
- 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>使用说明(转)
- <title>无题</title>
- 第五周 项目五--后缀表达式
- 数据结构之链表
- jquery的click事件重复执行问题
- zeptojs和jquery框架input disabled区别
- spring <context:component-scan>使用说明
- File操作-ObjectOutputStream实现对象的序列化
- Dialog、Activity和Fragment设置切换动画
- EJB到底是什么,真的那么神秘吗??
- 并发新特性—障碍器 CyclicBarrier
- 03 操作符
- 微信小程序(萌新请狠戳)
- http,socket,tcp/ip 网络传输与通讯知识总结
- spring事务配置的五种方式