这几天在看spring的源码,涉及到spring启动位置的部分,下面就看看spring到底是从哪儿开始加载的。本文使用的是spring3.0M3

首先spring的加载会借助一个监听器ContextLoaderListener,直接上web.xml文件

  1. <listener>
  2. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  3. </listener>

我们通常会对加载位置统一管理

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>
  4. /WEB-INF/conf/spring/**/*.xml
  5. </param-value>
  6. </context-param>

这个org.springframework.web.context.ContextLoaderListener类型是springframework中的原始加载上下文的监听器, 
通常我们会自定义一个Listener去继承ContextLoaderListener并另外实现我们需要初始化的接口(通常我们会选择实现一些接口来对session的管理)

  1. public class FrameServletContextListener extends ContextLoaderListener implements ServletContextListener,HttpSessionAttributeListener,HttpSessionListener {
  2. //
  3. private ServletContext initPath(ServletContextEvent event) {
  4. }
  5. public synchronized void contextDestroyed(ServletContextEvent event) {
  6. //
  7. }
  8. ...
  9. }

当监听器设置好了之后 ,启动web容器 监听器开始启动ContextLoaderListenerl 
类中的方法contextInitialized()

  1. /**
  2. * Initialize the root web application context.
  3. */
  4. public void contextInitialized(ServletContextEvent event) {
  5. this.contextLoader = createContextLoader();
  6. if (this.contextLoader == null) {
  7. this.contextLoader = this;
  8. }
  9. this.contextLoader.initWebApplicationContext(event.getServletContext());
  10. }

这样this.contextLoader.initWebApplicationContext(event.getServletContext());ContextLoaderListener 
就会借助容器的上下文去初始一个spring的应用上下文,使用到了ContextLoader这个类

在ContextLoader初始化时我们看到这样一块static代码

  1. static {
  2. // Load default strategy implementations from properties file.
  3. // This is currently strictly internal and not meant to be customized
  4. // by application developers.
  5. try {
  6. //这一句会去加载同在此包下的一个properties文件的值(ContextLoader.properties)
  7. ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
  8. defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
  9. }
  10. catch (IOException ex) {
  11. throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
  12. }
  13. }

属性文件中这样定义

引用
org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext

这样我们就能根据属性文件中的定义反射出一个XmlWebApplicationContext上下文了

然而我们在XmlWebApplicationContext中看到如下变量

  1. /** Default config location for the root context */
  2. public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";

至此我们已经知道默认加载spring文件的启动位置了

当我们再看ContextLoader类,我们就会看到传说中的参数contextConfigLocation

  1. public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";

而XmlWebApplicationContext对象正是调用了这个参数去设置启动位置

  1. wac.setConfigLocation(servletContext.getInitParameter(CONFIG_LOCATION_PARAM));

再往上看XmlWebApplicationContext继承的AbstractRefreshableConfigApplicationContext类中的setConfigLocation方法将此抽象类中的String[] configLocations值填充

并在AbstractRefreshableConfigApplicationContext类中我们看到spring对默认启动文件位置和配置启动文件位置的支持

  1. protected String[] getConfigLocations() {
  2. return (this.configLocations != null ? this.configLocations : getDefaultConfigLocations());


至此我们已经清楚spring将从哪儿加载并知道加载哪些文件了。

spring默认启动位置以及contextConfigLocation设置源码解析的更多相关文章

  1. 微服务架构 | *2.3 Spring Cloud 启动及加载配置文件源码分析(以 Nacos 为例)

    目录 前言 1. Spring Cloud 什么时候加载配置文件 2. 准备 Environment 配置环境 2.1 配置 Environment 环境 SpringApplication.prep ...

  2. Spring中AOP相关的API及源码解析

    Spring中AOP相关的API及源码解析 本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring ...

  3. abp vnext2.0核心组件之.Net Core默认DI组件切换到AutoFac源码解析

    老版Abp对Castle的严重依赖在vnext中已经得到了解决,vnext中DI容器可以任意更换,为了实现这个功能,底层架构相较于老版abp,可以说是进行了高度重构.当然这得益于.Net Core的D ...

  4. Spring核心框架 - AOP的原理及源码解析

    一.AOP的体系结构 如下图所示:(引自AOP联盟) 层次3语言和开发环境:基础是指待增加对象或者目标对象:切面通常包括对于基础的增加应用:配置是指AOP体系中提供的配置环境或者编织配置,通过该配置A ...

  5. Django的rest_framework认证组件之局部设置源码解析

    前言: Django的rest_framework组件的功能很强大,今天来我来给大家剖析一下认证组件 下面进入正文分析,我们从视图开始,一步一步来剖析认证组件 1.进入urls文件 url(r'^lo ...

  6. 【spring源码学习】spring的事件发布监听机制源码解析

    [一]相关源代码类 (1)spring的事件发布监听机制的核心管理类:org.springframework.context.event.SimpleApplicationEventMulticast ...

  7. [源码解析] PyTorch 分布式之弹性训练(2)---启动&单节点流程

    [源码解析] PyTorch 分布式之弹性训练(2)---启动&单节点流程 目录 [源码解析] PyTorch 分布式之弹性训练(2)---启动&单节点流程 0x00 摘要 0x01 ...

  8. Flink 源码解析 —— Standalone Session Cluster 启动流程深度分析之 Job Manager 启动

    Job Manager 启动 https://t.zsxq.com/AurR3rN 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0到1学习 -- Mac ...

  9. Flink 源码解析 —— Standalone session 模式启动流程

    Standalone session 模式启动流程 https://t.zsxq.com/EemAEIi 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0 ...

随机推荐

  1. 【1】第一次电话面试---上海EMC

    时间是2016//11月,投的是上海的EMC2公司的JavaWeb开发岗,第一次接到的电话面试,问的题目很基础基础,很遗憾,本人在掌握的太不好,回答的很乱,目测定挂.下面记下HR问的问题及回答. 首先 ...

  2. CDN的实现原理

    在描述CDN的实现原理,让我们先看传统的未加缓存服务的访问过程,以便了解CDN缓存访问方式与未加缓存访问方式的差别: 用户提交域名→浏览器对域名进行解释→得到目的主机的IP地址→根据IP地址访问发出请 ...

  3. C语言操作mysql

    php中 mysqli, pdo 可以用 mysqlnd 或 libmysqlclient 实现 前者 从 php 5.3.0起已内置到php中, 并且支持更多的特性,推荐用 mysqlnd mysq ...

  4. SG函数闲扯(转)

    http://ydcydcy1.blog.163.com/blog/static/216089040201342412717440/ 没来得及看.

  5. Unity3D 双摇杆 c# JoyStick 实现自己的双摇杆

    转载请声明: http://www.cnblogs.com/ADaii/p/3859520.html Untiy3D 官方摇杆脚本只能完成一个摇杆,要实现双摇杆比较费劲 现在我吧我废了一通宵的结果共享 ...

  6. 20. Candy && Gas Station

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  7. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  8. django model 中class meta

    class Meta: ordering = ['-num', 'length'] verbose_name = 'name' verbose_name_plural = 'names' orderi ...

  9. winform下自绘提示框风格窗体

    昨天分享了一个环形滚动条控件,今天分享一个提示框风格的窗体.代码如下: /// <summary> /// 继承自Form,但将FormBorderStyle设置为None /// < ...

  10. outlook找不到文件Outlook.pst 如何启动

    首先注明:这种情况在控制面板-邮件  无法打开的情况下可以使用以下命令打开设置界面 解决方法: 1. 开始 –> 运行 –> cmd 2. 在DOS下,用CD 切换到 Outlook.ex ...