tomcat启动时Spring做了什么(一、创建ContextLoader)

在配置Spring 的时候,我们需要在web.xml文件中配置一个listener如下:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
当server启动的时候,需要初始化WebApplicationContext,那么就会调用ContextLoaderListener的contextInitialized方法,该方法如下:
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}

[b]首先,它将创建ContextLoader;[/b]
那么在创建ContextLoader的时候会干什么呢?在ContextLoader里面,有一段静态块:
static {
// Load default strategy implementations from properties file.
// This is currently strictly internal and not meant to be customized
// by application developers.
try {
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
}
}

事实上,此段静态代码是为了获取WebApplicationContext的实现,WebApplicationContext的策略是定义在一个叫做ContextLoader.properties的文件中,Spring给了默认的定义,内容如下:
org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
一般来说,Spring团队建议不要去修改它,直接使用即可。
获取ContextLoader.properties的classPath后,将它用Properties包装。
接下来就是初始化WebApplicationContext了。
(待续)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值