spring在web中的启动是由ContextLoaderListener开始的。ContextLoaderListener实现了ServlContextListener接口,并继承了ContextLoader类。
笔记如下:
1.ServletContextListener 接口有两个方法:contextInitialized,contextDestroyed。
在服务器加载web应用的时候,这个Listener将被调用。
spring在contextInitialized中进行spring容器的初始化。
2.ContextLoader的initWebApplicationContext方法:
首先判断web应用中是否有spring容器被加载过,如下
如果有被加载过,将抛出异常。提示是否放置了多个ContextLoaderListener。
3.创建WebApplicationContext前,将先处理父上下文(对于ERA工程而言有用可能)。
普通的web项目,是没有父上下文的。
ApplicationContext parent = loadParentContext(servletContext);
下面这句是创建WebApplicationContext的主过程。
创建完的context置于servletContext中,这样Spring容器的宿主就是ServletContext(application Scope)。
4.createWebApplicationContext方法:
首先进行ApplicationContext Class的诊断:如果指定了ContextClass但没有实现ConfigurableWebApplicationContext的,将抛出异常。
默认将根据ContextLoader.properties中指定class的实例
即:org.springframework.web.context.support.XmlWebApplicationContext
之后根据Servlet的版本2.4之前及2.5的不同,获取web的DisplayName。
设置ConfigurableWebApplicationContext的id。
最后代码进入实质性的处理
- public class ContextLoaderListener extends ContextLoader implements ServletContextListener
public class ContextLoaderListener extends ContextLoader implements ServletContextListener
笔记如下:
1.ServletContextListener 接口有两个方法:contextInitialized,contextDestroyed。
在服务器加载web应用的时候,这个Listener将被调用。
spring在contextInitialized中进行spring容器的初始化。
- this.contextLoader.initWebApplicationContext(event.getServletContext());
this.contextLoader.initWebApplicationContext(event.getServletContext());
2.ContextLoader的initWebApplicationContext方法:
首先判断web应用中是否有spring容器被加载过,如下
- if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null)
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null)
如果有被加载过,将抛出异常。提示是否放置了多个ContextLoaderListener。
3.创建WebApplicationContext前,将先处理父上下文(对于ERA工程而言有用可能)。
普通的web项目,是没有父上下文的。
ApplicationContext parent = loadParentContext(servletContext);
下面这句是创建WebApplicationContext的主过程。
- this.context = createWebApplicationContext(servletContext, parent);
this.context = createWebApplicationContext(servletContext, parent);
创建完的context置于servletContext中,这样Spring容器的宿主就是ServletContext(application Scope)。
- servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
4.createWebApplicationContext方法:
首先进行ApplicationContext Class的诊断:如果指定了ContextClass但没有实现ConfigurableWebApplicationContext的,将抛出异常。
默认将根据ContextLoader.properties中指定class的实例
即:org.springframework.web.context.support.XmlWebApplicationContext
之后根据Servlet的版本2.4之前及2.5的不同,获取web的DisplayName。
设置ConfigurableWebApplicationContext的id。
最后代码进入实质性的处理
- wac.setParent(parent);
- wac.setServletContext(sc);
- //获取configLocation的配置
- wac.setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));
- customizeContext(sc, wac);// 用于自定义ContextClass时,扩充自定义Context
- wac.refresh();// 完成context构造过程。
wac.setParent(parent);
wac.setServletContext(sc);
//获取configLocation的配置
wac.setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));
customizeContext(sc, wac);// 用于自定义ContextClass时,扩充自定义Context
wac.refresh();// 完成context构造过程。