Spring IoC源码学习:ApplicationContext 刷新前的配置

本文详细解析了Spring IoC在ApplicationContext刷新前的配置过程,包括web.xml配置、defaultStrategies属性初始化,以及ContextLoaderListener的contextInitialized方法中的关键步骤,如创建WebApplicationContext、初始化Environment等。
摘要由CSDN通过智能技术生成

微信搜索【程序员囧辉】,关注这个坚持分享技术干货的程序员。

目录

Spring IoC源码学习全系列

前言

正文

web.xml 配置

defaultStrategies 属性初始化

contextInitialized方法

代码块1:createWebApplicationContext

代码块2:determineContextClass

代码块3:configureAndRefreshWebApplicationContext

代码块4:wac.setConfigLocation

代码块5:resolvePath

代码块6:getEnvironment

代码块7:createEnvironment

代码块8:new StandardServletEnvironment()

代码块9:customizePropertySources

代码块10:customizePropertySources

代码块11:initPropertySources

代码块12:customizeContext

代码块13:determineContextInitializerClasses

代码块14:loadInitializerClass

customizeContext方法扩展

总结

相关文章


Spring IoC源码学习全系列

小白也看得懂的 Spring IoC 核心流程介绍

Spring IoC源码学习:总览

Spring IoC源码学习:ApplicationContext 刷新前的配置

Spring IoC源码学习:obtainFreshBeanFactory详解

Spring IoC源码学习:parseDefaultElement详解

Spring IoC源码学习:parseCustomElement详解

Spring IoC源码学习:context:component-scan 节点详解

Spring IoC源码学习:invokeBeanFactoryPostProcessors详解

Spring IoC源码学习:registerBeanPostProcessors详解

Spring IoC源码学习:finishBeanFactoryInitialization详解

Spring IoC源码学习:getBean详解

Spring IoC源码学习:createBean详解(上)

Spring IoC源码学习:createBean详解(下)

Spring IoC源码学习:@Autowire 详解

Spring IoC源码学习:finishRefresh 详解

 

前言

Spring IoC:源码学习总览 中,我们简单介绍了 IoC 过程最重要的一个方法,也就是 AbstractApplicationContext#refresh() 方法,在正式学习 refresh 方法之前,还有一些刷新前的操作比较重要,本文将对这部分内容进行介绍。

 

正文

web.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>open-joonwhee-service WAR</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:config/spring/appcontext-*.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

该 web.xml 是一个使用了 Spring 框架的项目的最基本的配置,配置了 ContextLoaderListener 和 contextConfigLocation。其中 ContextLoaderListener 是 Spring 的入口,而 contextConfigLocation 是 Spring 配置文件的路径。

接下来,让我们从 ContextLoaderListener#contextInitialized 开始 IoC 的构建。

进入 ContextLoaderListener#contextInitialized 方法之前,由于 ContextLoaderListener 继承了 ContextLoader,需要先将 ContextLoader 的成员变量初始化。在 ContextLoader 的成员变量中,defaultStrategies 属性的初始化比较重要,下面拿出来单独介绍。

 

defaultStrategies 属性初始化

private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties";


private static final Properties defaultStrategies;

static {
    // Load default strategy implementations from properties file.
    // This is currently strictly internal and not meant to be customized
    // by application developers.
    try {
        // 1.根据 DEFAULT_STRATEGIES_PATH(ContextLoader.properties) 和 ContextLoader.class 构建 ClassPathResource,
        // path在这边为相对路径,全路径为:org.springframework.web.context.ContextLoader.properties
        ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
        // 2.加载resource的属性,在这边我们拿到了默认的WebApplicationContext,即:XmlWebApplicationContext
        defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
    } catch (IOException ex) {
        throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
    }
}

1.根据 DEFAULT_STRATEGIES_PATH 和 ContextLoader.class 构建 ClassPathResource。ClassPathResource 的 path 属性可以是绝对路径也可以是相对路径,在这边为相对路径(相对于加载资源的类 ContextLoader),指向的绝对路径为:org.springframework.web.context.ContextLoader.properties。

2.加载 resource 属性,并赋值给 defaultStrategies。根据 org.springframework.web.context.ContextLoader.properties 路径找到对应的文件,如下图。在这边我们拿到了默认的 WebApplicationContext,即:XmlWebApplicationContext,如下图所示。

</

  • 13
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员囧辉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值