ContextLoaderListener加载过程(最详细版)

https://blog.youkuaiyun.com/MrZhangXL/article/details/78587426

以下描述,将是最详细版spring的ContextLoaderListener加载过程,没有之一。

ContextLoaderListener实现了ServletContextListener接口,ServletContextListener是Java EE标准接口之一,类似tomcat,jetty的java容器启动时便会触发该接口的contextInitialized。

1 顾,java容器启动触发ContextLoaderListener的contextInitialized

2 contextInitialized 方法调用ContextLoader的initWebApplicationContext方法。

3 initWebApplicationContext调用createWebApplicationContext方法

4 createWebApplicationContext 调用determineContextClass方法

5 determineContextClass有如下代码

contextClassName = defaultStrategies
                .getProperty(WebApplicationContext.class.getName());



显然是从defaultStrategies中加载的

ContextLoader 类中有段静态代码

static {
        try {
            ClassPathResource resource = new ClassPathResource(
                    "ContextLoader.properties", ContextLoader.class);
            defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException ex) {
            throw new IllegalStateException(
                    "Could not load 'ContextLoader.properties': "
                            + ex.getMessage());
        }

        currentContextPerThread = new ConcurrentHashMap(1);
    }



ContextLoader.properties 文件内容如下:

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

至此,determineContextClass方法返回的是XmlWebApplicationContext

6 回到 initWebApplicationContext 方法,调用configureAndRefreshWebApplicationContext方法

7 configureAndRefreshWebApplicationContext 调用了AbstractApplicationContext的refresh方法

8 refresh 方法调用了obtainFreshBeanFactory

9 obtainFreshBeanFactory 调用了AbstractRefreshableApplicationContext类的refreshBeanFactory方法

10 refreshBeanFactory调用了XmlWebApplicationContext的loadBeanDefinitions

11 loadBeanDefinitions中加载了对应的applicationContext.xml

如果你还没看明白,请联系我。
--------------------- 
作者:工匠解码 
来源:优快云 
原文:https://blog.youkuaiyun.com/MrZhangXL/article/details/78587426 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hello_world!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值