如何精准控制Spring ApplicationContext的扫描范围
来源:互联网 发布:淘宝描述不符的处罚 编辑:程序博客网 时间:2024/05/29 16:59
在Spring中,扫描的方式一是基于XML,二是基于Annotation,它们既可以混合使用,又可以隔离使用,但在技术框架的使用中,需要精准地控制它们的扫描范围,一方面可以避免重复注册,另一方面可以避免漏掉核心的服务Bean。
更重要的是,在诸如事务控制的场景中,精准地控制ApplicationContext的扫描一方面可以提高系统性能,加快程序启动速度,另一方面可以避免操作范围混淆,尤其是在Spring 3及之前的版本,必须隔离Web ApplicationContext与Root ApplicationContext,否则事务将无法成功。
下面以XML为例,控制Controller的扫描范围
<context:component-scan base-package="com.stixu" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/> </context:component-scan>
上面的代码片段将ApplicationContext扫描范围局限在Controller与RestController上,需要注意的是,即使此时系统中有@Configuration的类,其定义的@Bean也无法激活。
如果要激活@Configuration,需要在配置中添加如下片段:
<context:component-scan base-package="com.stixu" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/> <!-- 激活@Configuration --> <context:include-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/> </context:component-scan>
而在@Configuration中激活XML配置,就太简单了,代码如下:
// 基于Annotation的配置@Configuration// XML中的Bean将自动激活@ImportResource({"classpath:/META-INF/springContext/mvc-context.xml", "classpath:/META-INF/springContext/booter-context.xml" })public class AppConfig {}
结论
精准控制Spring ApplicationContext的扫描范围可以提高程序的性能,其关键配置是context:include-filter,但要注意,别忽略了@Configuration与XML配置的差异与联系。
阅读全文
0 0
- 如何精准控制Spring ApplicationContext的扫描范围
- spring的applicationContext.xml如何自动加载
- spring 如何得到applicationContext
- spring 如何加载applicationContext.xml
- spring不懂的applicationContext
- spring 的 applicationcontext.xml
- Spring的applicationContext.xml
- spring 的 applicationcontext.xml
- Spring的applicationContext.xml
- Spring ApplicationContext的应用
- spring的applicationContext.xml
- spring的applicationContext.xml
- Spring的Applicationcontext配置文件
- spring的applicationContext模板
- Spring的ApplicationContext
- 如何在Java Web应用中获取Spring的ApplicationContext
- Spring -- 如何为applicationContext.xml 添加 util 的 *.xsd文件
- ZBarReaderView的设定扫描范围
- Kotlin创建的小Demo
- 二叉树相关算法
- 数据结构期末考试再复习
- 深度强化学习--第一讲
- Softmax学习笔记
- 如何精准控制Spring ApplicationContext的扫描范围
- android Permission denied
- 关于属性传值为空的问题
- 排序相关算法
- META标签特效(页面过渡效果)
- 环形链表
- java应用基础
- 电话号码管理程序
- 从HashMap到ConcurrentHashMap