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

本文详细介绍了Spring IoC容器在初始化ApplicationContext时的流程,包括加载配置、创建WebApplicationContext、配置和刷新上下文、设置属性等步骤。重点解析了如何从'ContextLoader.properties'加载默认策略,以及如何根据web.xml配置初始化上下文。通过对关键代码块的分析,揭示了Spring在启动时如何处理配置和初始化细节。
摘要由CSDN通过智能技术生成

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,如下图所示。

接着,我们正式进入 ContextLoaderListener#contextInitialized 方法。

contextInitialized方法

====================

@Override

public void contextInitialized(ServletContextEvent event) {

initWebApplicationContext(event.getServletContext());

}

public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {

// 1.校验WebApplicationContext 是否已经初始化过,如果已经初始化,则抛出异常

if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {

throw new IllegalStateException(

"Cannot initialize context because there is already a root application context present - " +

“check whether you have multiple ContextLoader* definitions in your web.xml!”);

}

Log logger = LogFactory.getLog(ContextLoader.class);

servletContext.log(“Initializing Spring root WebApplicationContext”);

if (logger.isInfoEnabled()) {

logger.info(“Root WebApplicationContext: initialization started”);

}

long startTime = System.currentTimeMillis();

try {

// Store context in local instance variable, to guarantee that

// it is available on ServletContext shutdown.

if (this.context == null) {

// 2.创建一个WebApplicationContext并保存到context属性

this.context = createWebApplicationContext(servletContext);

}

if (this.context instanceof ConfigurableWebApplicationContext) {

ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;

if (!cwac.isActive()) {

// The context has not yet been refreshed -> provide services such as

// setting the parent context, setting the application context id, etc

if (cwac.getParent() == null) {

// The context instance was injected without an explicit parent ->

// determine parent for root web application context, if any.

ApplicationContext parent = loadParentContext(servletContext);

cwac.setParent(parent);

}

// 3.配置和刷新web应用上下文

configureAndRefreshWebApplicationContext(cwac, servletContext);

}

}

// 4.设置WebApplicationContext属性

servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

ClassLoader ccl = Thread.currentThread().getContextClassLoader();

if (ccl == ContextLoader.class.getClassLoader()) {

currentContext = this.context;

} else if (ccl != null) {

currentContextPerThread.put(ccl, this.context);

}

if (logger.isDebugEnabled()) {

logger.debug(“Published root WebApplicationContext as ServletContext attribute with name [” +

WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + “]”);

}

if (logger.isInfoEnabled()) {

long elapsedTime = System.currentTimeMillis() - startTime;

logger.info(“Root WebApplicationContext: initialization completed in " + elapsedTime + " ms”);

}

return this.context;

} catch (RuntimeException ex) {

logger.error(“Context initialization failed”, ex);

servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);

throw ex;

} catch (Error err) {

logger.error(“Context initialization failed”, err);

servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);

throw err;

}

}

2.创建一个 WebApplicationContext,见代码块1详解

  • 22
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值