spring源码解析之默认配置文件名/WEB-INF/applicationContext.xml

本文探讨了Spring框架中默认配置文件`WEB-INF/applicationContext.xml`的源码解析过程。从`web.xml`中`ContextLoaderListener`出发,深入到`ContextLoader.java`,揭示了如何初始化`webApplicationContext`对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

众所周之,spring源码解析之默认配置文件名/WEB-INF/applicationContext.xml。但是这个是在spring源代码里记录的呢?

今天就抱着试一试的态度去找到答案。


首先查看工程中的 web.xml,其中配置 spring的代码如下

 	<context-param> 
 		<param-name>contextConfigLocation</param-name> 
 		<param-value>/WEB-INF/applicationContext.xml</param-value> 
 		<description>Spring配置文件初始化WebApplicationContext</description>
 		<contextClass></contextClass> <!-- 自定义 -->
 	</context-param> 
 	
	
 <!-- spring 配置 对应的是     [org.springframework.web] 属于spring-web JAR包  applicationContext.xml  汇总多个 配置文件-->
 	<!-- spring 装载器 -->
   <listener>  
 		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
   </listener> 

其中   参数名    contextConfigLocation 就是描述spring配置文件初始化的位置。

那咱们就从 spring 装载器    listener-class 的  ContextLoaderListener.java 查询结果

 这里有 spring-web.jar的源码查看地址:其中描述了需要通过  ContextLoader.java 来初始化 webApplicationContext对象

public void contextInitialized(ServletContextEvent event) {
        this.contextLoader = createContextLoader();
        if (this.contextLoader == null) {
            this.contextLoader = this;
        }
        this.contextLoader.initWebApplicationContext(event.getServletContext());
    }

jar包中   包含对应的配置文件 ContextLoader.properties 文件

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

那么好吧,我们将在  XmlWebApplicationContext.java  文件中找到我们想要的答案!

  

 获取默认配置文件名:
     protected String[] getDefaultConfigLocations()
    {
        if(getNamespace() != null)
            return (new String[] {
                (new StringBuilder("/WEB-INF/")).append(getNamespace()).append(".xml").toString()
            });
        else
            return (new String[] {
                "/WEB-INF/applicationContext.xml"
            });
    }
如果为null,没有指定文件名的话,那么就是默认的  /applicationContext.xml 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值