1.web.xml中的常见配置
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:followMe.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>com.heng.listener.MyContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:followMe.xml</param-value>
</context-param>
2.Web容器启动的部分流程,如tomcat
- 加载解析web.xml中的配置信息
- 初始化listener组件,调用
contextInitialized(ServletContextEvent event)
方法。
调用链:StandardContext.listenerStart()
>ContextLoaderListener.contextInitialized(ServletContextEvent)
1.创建WebApplicationContext
createWebApplicationContext(ServletContext sc)
WebApplicationContext的Class类型:
a.取web容器上下文中contextClass
的值。sc.getInitParameter("contextClass")
b.取ContextLoader.properties中默认值XmlWebApplicationContext
2.配置刷新WebApplicationContet
a.设置parent容器parent=loadParentContext(sc) wac.setParent(parent)
b.设置容器ID,使用sc.getInitParameter("contextId")
的值或生成
c.将web容器上下文设置到容器wac.setServletContext(sc)
d.设置配置文件读取路径 详解configLocation
configLocationParam = sc.getInitParameter( "contextConfigLocation")
wac.setConfigLocation(configLocationParam)
e.将web容器上下文中的一些信息设置到environment中
f.在WebApplicationContet刷新前对它进行一些定制customizeContext(sc, wac)
g.刷新WebApplicationContetwac.refresh();
3.将WebApplicationContext设置到web容器上下文中
sc.setAttribute("org.springframework.web.context.WebApplicationContext.ROOT" ,context)
- 初始化Servlet组件,调用
init()
方法。
调用链:StandardWrapper.initServlet(Servlet)
>DispatcherServlet(GenericServlet).init(ServletConfig)
>
DispatcherServlet(HttpServletBean).init()
1.读取ServletConfig中的配置参数设置到DispatcherServlet(FrameServlet)中
2.配置刷新WebApplicationContet,大致过程如上
特殊点:会注册一个静态指定的监听器,
wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));
,这样在后面容器刷新时registerListeners()
方法就会把该监听器添加到事件传播器SimpleApplicationEventMulticaster中。