使用ContextLoaderListener也可配置SpringMVC
web.xml配置文件如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>sun</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<!-- 指定Spring配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
进入容器加载监听器ContextLoaderListener类中,找到第107行的initWebApplicationContext方法。
进入该方法声明类ContextLoader中找到该方法,即初始化Web容器
最后初始化完毕后返回当前容器。
------------------------------------------------↓↓↓init方法片段分析↓↓↓--------------------------------------------------------
在上面两段代码片段中,其中createWebApplicationContext方法是通过反射获取wac对象,源码如下:
在配置并刷新wac容器时,调用的configureAndRefreshWebApplicationContext方法源码如下:
其中在赋值给key时,我们可以看一下ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE它的全称是什么
可以看出该key的全称是org.springframework.web.context.WebApplicationContext.ROOT
---------------------------------------↓↓↓使用拦截器配置mvc注意事项↓↓↓-------------------------------------------------
思考:为什么使用contextListener监听器配置mvc的话,我们的web.xml配置中有下面这么两行配置代码?
我们从分析DispatcherServlet类入手,
由于我们配置了<load-on-startup>标签,因此DispatcherServlet会依次执行init方法—>initServletBean()方法—>initWebApplicationContext()方法
《load-on-startup标签的作用》博客链接:load-on-startup标签的作用
源码如下:
protected WebApplicationContext initWebApplicationContext() {
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;
if (this.webApplicationContext != null) {
// A context instance was injected at construction time -> use it
wac = this.webApplicationContext;
if (wac instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent -> set
// the root application context (if any; may be null) as the parent
cwac.setParent(rootContext);
}
configureAndRefreshWebApplicationContext(cwac);
}
}
}
if (wac == null) {
// No context instance was injected at construction time -> see if one
// has been registered in the servlet context. If one exists, it is assumed
// that the parent context (if any) has already been set and that the
// user has performed any initialization such as setting the context id
wac = findWebApplicationContext();
}
if (wac == null) {
// No context instance is defined for this servlet -> create a local one
wac = createWebApplicationContext(rootContext);
}
if (!this.refreshEventReceived) {
// Either the context is not a ConfigurableApplicationContext with refresh
// support or the context injected at construction time had already been
// refreshed -> trigger initial onRefresh manually here.
onRefresh(wac);
}
if (this.publishContext) {
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
"' as ServletContext attribute with name [" + attrName + "]");
}
}
return wac;
}