spring默认启动位置以及contextConfigLocation设置源码解析
这几天在看spring的源码,涉及到spring启动位置的部分,下面就看看spring到底是从哪儿开始加载的。本文使用的是spring3.0M3
首先spring的加载会借助一个监听器ContextLoaderListener,直接上web.xml文件
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
我们通常会对加载位置统一管理
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/conf/spring/**/*.xml
- </param-value>
- </context-param>
这个org.springframework.web.context.ContextLoaderListener类型是springframework中的原始加载上下文的监听器,
通常我们会自定义一个Listener去继承ContextLoaderListener并另外实现我们需要初始化的接口(通常我们会选择实现一些接口来对session的管理)
- public class FrameServletContextListener extends ContextLoaderListener implements ServletContextListener,HttpSessionAttributeListener,HttpSessionListener {
- //
- private ServletContext initPath(ServletContextEvent event) {
- }
- public synchronized void contextDestroyed(ServletContextEvent event) {
- //
- }
- ...
- }
当监听器设置好了之后 ,启动web容器 监听器开始启动ContextLoaderListenerl
类中的方法contextInitialized()
- /**
- * Initialize the root web application context.
- */
- public void contextInitialized(ServletContextEvent event) {
- this.contextLoader = createContextLoader();
- if (this.contextLoader == null) {
- this.contextLoader = this;
- }
- this.contextLoader.initWebApplicationContext(event.getServletContext());
- }
这样this.contextLoader.initWebApplicationContext(event.getServletContext());ContextLoaderListener
就会借助容器的上下文去初始一个spring的应用上下文,使用到了ContextLoader这个类
在ContextLoader初始化时我们看到这样一块static代码
- static {
- // Load default strategy implementations from properties file.
- // This is currently strictly internal and not meant to be customized
- // by application developers.
- try {
- //这一句会去加载同在此包下的一个properties文件的值(ContextLoader.properties)
- ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
- defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
- }
- catch (IOException ex) {
- throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
- }
- }
属性文件中这样定义
这样我们就能根据属性文件中的定义反射出一个XmlWebApplicationContext上下文了
然而我们在XmlWebApplicationContext中看到如下变量
- /** Default config location for the root context */
- public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
至此我们已经知道默认加载spring文件的启动位置了
当我们再看ContextLoader类,我们就会看到传说中的参数contextConfigLocation
- public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";
而XmlWebApplicationContext对象正是调用了这个参数去设置启动位置
- wac.setConfigLocation(servletContext.getInitParameter(CONFIG_LOCATION_PARAM));
再往上看XmlWebApplicationContext继承的AbstractRefreshableConfigApplicationContext类中的setConfigLocation方法将此抽象类中的String[] configLocations值填充
并在AbstractRefreshableConfigApplicationContext类中我们看到spring对默认启动文件位置和配置启动文件位置的支持
- protected String[] getConfigLocations() {
- return (this.configLocations != null ? this.configLocations : getDefaultConfigLocations());
}
至此我们已经清楚spring将从哪儿加载并知道加载哪些文件了。
spring默认启动位置以及contextConfigLocation设置源码解析的更多相关文章
- 微服务架构 | *2.3 Spring Cloud 启动及加载配置文件源码分析(以 Nacos 为例)
目录 前言 1. Spring Cloud 什么时候加载配置文件 2. 准备 Environment 配置环境 2.1 配置 Environment 环境 SpringApplication.prep ...
- Spring中AOP相关的API及源码解析
Spring中AOP相关的API及源码解析 本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring ...
- abp vnext2.0核心组件之.Net Core默认DI组件切换到AutoFac源码解析
老版Abp对Castle的严重依赖在vnext中已经得到了解决,vnext中DI容器可以任意更换,为了实现这个功能,底层架构相较于老版abp,可以说是进行了高度重构.当然这得益于.Net Core的D ...
- Spring核心框架 - AOP的原理及源码解析
一.AOP的体系结构 如下图所示:(引自AOP联盟) 层次3语言和开发环境:基础是指待增加对象或者目标对象:切面通常包括对于基础的增加应用:配置是指AOP体系中提供的配置环境或者编织配置,通过该配置A ...
- Django的rest_framework认证组件之局部设置源码解析
前言: Django的rest_framework组件的功能很强大,今天来我来给大家剖析一下认证组件 下面进入正文分析,我们从视图开始,一步一步来剖析认证组件 1.进入urls文件 url(r'^lo ...
- 【spring源码学习】spring的事件发布监听机制源码解析
[一]相关源代码类 (1)spring的事件发布监听机制的核心管理类:org.springframework.context.event.SimpleApplicationEventMulticast ...
- [源码解析] PyTorch 分布式之弹性训练(2)---启动&单节点流程
[源码解析] PyTorch 分布式之弹性训练(2)---启动&单节点流程 目录 [源码解析] PyTorch 分布式之弹性训练(2)---启动&单节点流程 0x00 摘要 0x01 ...
- Flink 源码解析 —— Standalone Session Cluster 启动流程深度分析之 Job Manager 启动
Job Manager 启动 https://t.zsxq.com/AurR3rN 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0到1学习 -- Mac ...
- Flink 源码解析 —— Standalone session 模式启动流程
Standalone session 模式启动流程 https://t.zsxq.com/EemAEIi 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0 ...
随机推荐
- JAVA_Java常用核心包(概念)
20150802 Created By BaoXinjian
- (转自http://www.blogjava.net/moxie/archive/2006/10/20/76375.html)WebWork深入浅出
(转自http://www.blogjava.net/moxie/archive/2006/10/20/76375.html) WebWork深入浅出 本文发表于<开源大本营> 作者:钱安 ...
- 华为OJ题目:扑克牌大小
题目描述: 扑克牌游戏大家应该都比较熟悉了,一副牌由54张组成,含3~A.2各4张,小王1张,大王1张.牌面从小到大用如下字符和字符串表示(其中,小写joker表示小王,大写JOKER表示大王):3 ...
- noip2008解题报告
T1.笨小猴 给出一个单词求出现次数最多和最少之差是不是质数. 很水的.统计一下反正就26个字母. T2.火柴棒等式 给出火柴棒数,求形如 a+b=c能拼成的等式个数. 先减去4根(+,=),然后枚举 ...
- jsp 错误码debug记录与总结
500: 编码错误: 无法向cookie中写入中文字符串 需要使用URLEncoder.Encode()在写入处进行转码,使用URLDecoder.decoder()在读取处进行解码 或者使用requ ...
- shell字符串判空
主要用到两个命令 -n -z if [ -n "$PID" ]; then echo "PID is not empty" fi if[ -z "$ ...
- string和vector
一.String对象 1.string s; s.size(); //返回的是s中字符的个数,也是s的长度: //string对象最后没有加空字符 //size()返回的是string::s ...
- (知识分享)软硬件调试九法:第九条规则 如果你不修复一个bug,它将永远存在
1.查证问题确已被修复 如果遵循了“制造失败”这条规则,就知道如何验证你确实修复了问题.无论问题和修复看起来多么明显,你都无法保证修复是有效的,直到做了测试并验证. 2.查证确实你的修复措施解决了问题 ...
- ffmpeg去logo<转>
用到 video filter —— delogo 通过周围像素插值去除 logo. 参数介绍: x y (必须)指定 logo 的坐标. w h (必须)指定 logo 的宽和高. band, t ...
- Reactjs 入门基础(二)
如果我们需要向组件传递参数,可以使用 this.props 对象,实例如下: <body> <div id="example"></div> & ...