【1】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" xmlns:web="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_3_0.xsd" version="3.0"> <!-- <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> --> <listener> <listener-class>com.corn.core.spring.SpringIocContextListener </listener-class> </listener> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>corn-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>namespace</param-name> <param-value>corn-mvc</param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </init-param> <init-param> <param-name>publishContext</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>corn-mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy </filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login.do</welcome-file> </welcome-file-list> </web-app>
如上配置,可不用配置注释掉的代码:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
为什么可以去掉ContextLoaderListener监听器呢?
首先看该监听器源码:
/* * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.web.context; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; /** * Bootstrap listener to start up and shut down Spring's root {@link WebApplicationContext}. * Simply delegates to {@link ContextLoader} as well as to {@link ContextCleanupListener}. * * <p>This listener should be registered after {@link org.springframework.web.util.Log4jConfigListener} * in {@code web.xml}, if the latter is used. * * <p>As of Spring 3.1, {@code ContextLoaderListener} supports injecting the root web * application context via the {@link #ContextLoaderListener(WebApplicationContext)} * constructor, allowing for programmatic configuration in Servlet 3.0+ environments. * See {@link org.springframework.web.WebApplicationInitializer} for usage examples. * * @author Juergen Hoeller * @author Chris Beams * @since 17.02.2003 * @see #setContextInitializers * @see org.springframework.web.WebApplicationInitializer * @see org.springframework.web.util.Log4jConfigListener */ public class ContextLoaderListener extends ContextLoader implements ServletContextListener { /** * Create a new {@code ContextLoaderListener} that will create a web application * context based on the "contextClass" and "contextConfigLocation" servlet * context-params. See {@link ContextLoader} superclass documentation for details on * default values for each. * <p>This constructor is typically used when declaring {@code ContextLoaderListener} * as a {@code <listener>} within {@code web.xml}, where a no-arg constructor is * required. * <p>The created application context will be registered into the ServletContext under * the attribute name {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} * and the Spring application context will be closed when the {@link #contextDestroyed} * lifecycle method is invoked on this listener. * @see ContextLoader * @see #ContextLoaderListener(WebApplicationContext) * @see #contextInitialized(ServletContextEvent) * @see #contextDestroyed(ServletContextEvent) */ public ContextLoaderListener() { } /** * Create a new {@code ContextLoaderListener} with the given application context. This * constructor is useful in Servlet 3.0+ environments where instance-based * registration of listeners is possible through the {@link javax.servlet.ServletContext#addListener} * API. * <p>The context may or may not yet be {@linkplain * org.springframework.context.ConfigurableApplicationContext#refresh() refreshed}. If it * (a) is an implementation of {@link ConfigurableWebApplicationContext} and * (b) has <strong>not</strong> already been refreshed (the recommended approach), * then the following will occur: * <ul> * <li>If the given context has not already been assigned an {@linkplain * org.springframework.context.ConfigurableApplicationContext#setId id}, one will be assigned to it</li> * <li>{@code ServletContext} and {@code ServletConfig} objects will be delegated to * the application context</li> * <li>{@link #customizeContext} will be called</li> * <li>Any {@link org.springframework.context.ApplicationContextInitializer ApplicationContextInitializer}s * specified through the "contextInitializerClasses" init-param will be applied.</li> * <li>{@link org.springframework.context.ConfigurableApplicationContext#refresh refresh()} will be called</li> * </ul> * If the context has already been refreshed or does not implement * {@code ConfigurableWebApplicationContext}, none of the above will occur under the * assumption that the user has performed these actions (or not) per his or her * specific needs. * <p>See {@link org.springframework.web.WebApplicationInitializer} for usage examples. * <p>In any case, the given application context will be registered into the * ServletContext under the attribute name {@link * WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} and the Spring * application context will be closed when the {@link #contextDestroyed} lifecycle * method is invoked on this listener. * @param context the application context to manage * @see #contextInitialized(ServletContextEvent) * @see #contextDestroyed(ServletContextEvent) */ public ContextLoaderListener(WebApplicationContext context) { super(context); } /** * Initialize the root web application context. */ @Override public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); } /** * Close the root web application context. */ @Override public void contextDestroyed(ServletContextEvent event) { closeWebApplicationContext(event.getServletContext()); ContextCleanupListener.cleanupAttributes(event.getServletContext()); } }
根据源码可知,该监听器在web容器启动时,initWebApplicationContext;在web容器销毁时,closeWebApplicationContext。
DispatcherServlet 配置参数解析:
namespace:
DispatcherServlet对应的命名空间,用以构造Spring配置文件的路径,如果指定该属性后,默认配置文件对应的路径为:WEB-INF/<namespace>.xml而非WEB-INF/< servlet-name>-servlet.xml。
contextConfigLocation:
如果DispatcherServlet上下文对应的Spring配置文件有多个,则可以使用该属性按照Spring资源路径的方式进行指定。
.
如设置为“classpath:applicationContext.xml”时,DispatcherServlet将
publishContext:
boolean类型属性,默认值为ture。
.
DispatcherServlet据此属性决定是否将对应的WebApplicationContext发布到ServletContext容器中,以便任何其它的Bean可以通过ServletContext找到DispatcherServlet上下文对应的WebApplicationContext。
.
对应的属性名为DispatcherServlet#getServletContextAttributeName()方法返回的值。
publishEvents:
boolean类型属性。当DispatcherServlet处理完一个请求后,是否需要向容器发布一个ServletRequestHandledEvent事件,默认为ture。
.
如果容器中没有任何事件监听器,可以将此属性设置为false,以便赚取一些程序运行性能。
即,如上DispatcherServlet配置,容器中同样会有WebApplicationContext供你获取bean使用。
自定义的SpringIocContextListener(可不配置)如下:
public class SpringIocContextListener implements ServletContextListener { private final static Logger log = LoggerFactory.getLogger(SpringIocContextListener.class); private static ServletContext servletcontext; public void contextDestroyed(ServletContextEvent arg0) { servletcontext=null; log.info("SpringIocContextListener Destroy success!"); } public void contextInitialized(ServletContextEvent arg0) { servletcontext = arg0.getServletContext(); log.info("SpringIocContextListener Initialize success!"); } public static ServletContext getServletcontext(){ return servletcontext; } }
【2】web.xml各节点加载顺序
1.启动WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点:
<listener></listener> 和 <context-param></context-param>
2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.
3.容器将<context-param></context-param>转化为键值对,并交给ServletContext.
4.容器将创建<listener>中的类实例,创建监听。
5.容器将创建<filter>中的类实例,创建过滤器。
6.容器将加载load-on-startup为1的servlet。
<load-on-startup>1</load-on-startup>
未配置load-on-startup的servlet将在第一次收到请求时初始化。
需要注意的是,系统中不同类型节点加载顺序与配置顺序无关。同一元素的加载是按顺序加载的。
例如filter与filter-mapping,必须先定义filter,才能正确解析对应名字的filter-mapping。
另外需要注意同类节点配置顺序的强制要求,如下:
综上,web.xml中加载顺序为:context-param -> listener -> filter -> servlet ,而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的。
加载顺序与它们在 web.xml 文件中的先后顺序无关,但默认按次序从上到下依次配置。
如果再加上拦截器之后的顺序呢?
拦截器是在Spring MVC中配置的,如果从整个项目中看,一个servlet请求的执行过程就变成了这样context-param–>listener–>filter–>servlet–>interceptor(指的是拦截器),为什么拦截器是在servlet执行之后,因为拦截器本身依赖与Servlet容器。