接着从入门到放弃(1)-tomcat -spring web.xml继续对spring启动的探索
上文讲到
ServletContextListener listener =
(ServletContextListener) instances[i];
listener.contextInitialized(event);
调用到我们项目即可找到,idea 快捷键ctrl+shift+n 打开对应的类
获取类的路径:copy referene : org.springframework.web.context.ContextLoader
找到 org.springframework.web.context.ContextLoader源码,找到285行打印日志处,可以看到打印了info lever 日志,并且可以找到对应的方法org.springframework.web.context.ContextLoader.initWebApplicationContext(ServletContext servletContext)
先看下org.springframework.web.context.ContextLoader.initWebApplicationContext(ServletContext servletContext)是从哪里调用进来的,熟悉idea同学可以知道ctrl+鼠标左键可以看到在这个方法在哪里调用
进入org.springframework.web.context.ContextLoaderListener.contextInitialized(ServletContextEvent event)的112行,可以找到对应的调用位置,详细的contextInitialized(ServletContextEvent event)源码
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
if (this.contextLoader == null) {
this.contextLoader = this;
}
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
因为我三个listen中第一个listen 继承了ContextLoaderListener 并且调用了 super.contextInitialized(event);
查看当前类的关系,public class ContextLoaderListener extends ContextLoader implements ServletContextListener,继承于ContextLoader 实现于ServletContextListener,这与上篇讲的tomcat启动web.xml 正好合到一起了。并且把event 从tomcat容器成功传递到spring 中
先看下ContextLoader 加载的内容
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 {
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex)