欢迎使用CSDN-markdown编辑器

来源:互联网 发布:wear of it 编辑:程序博客网 时间:2024/05/25 01:35

SpringMVC 流程分析
一. 常用web.xml配置

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4"        xmlns="http://java.sun.com/xml/ns/j2ee"             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    <!--注意, listener和filter直接放置在web.xml顶端-->    <!-- ContextLoaderListener -->    <listener>        <listener-class>                org.springframework.web.context.**ContextLoaderListener**        </listener-class>    </listener>    <context-param>        <param-name>**contextConfigLocation**</param-name>        <param-value>            classpath:spring/**applicationContext**.xml        </param-value>    </context-param>    <!-- DispatcherServlet,Spring MVC looks for a file named [servlet-name]-servlet.xml-->    <servlet>        <servlet-name>dispatcher</servlet-name>        <servlet-class>            org.springframework.web.servlet.**DispatcherServlet**        </servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>dispatcher</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.html</url-pattern>    </servlet-mapping></web-app>

web.xml配置了ContextLoaderListener,contextConfigLocation(classpath:spring/applicationContext.xml)和DispatcherServlet。

二. 服务器启动阶段(DispatcherServlet初始化阶段)
1. 启动后ContextLoaderListener首先会在初始化方法contextInitialized(ServletContextEvent event)中调用
initWebApplicationContext(event.getServletContext())来在创建一个上下文WebApplicationContext,
并将其作为ServletContext的一个属性设置到Servlet环境中。
这个上下文对象为根上下文对象(root WebApplicationContext)。

ContextLoaderListener类代码如下:

//ContextLoaderListener继承自ContextLoader,实现了ServletContextListener//Bootstrap listener to start up and shut down Spring's root WebApplicationContext.public class ContextLoaderListener extends ContextLoader  implements ServletContextListener{   ……   //Initialize the root web application context.   //ServletContextEvent:This is the event class(事件类) for notifications about changes to the servlet context of a web application.   pulic void contextInitialized(ServletContextEvent event) {     this.contextLoader = createContextLoader();     if (this.contextLoader == null) {           this.contextLoader = this;     }   this.contextLoader.initWebApplicationContext(event.getServletContext());  } ……}

ContextLoader类代码如下:

/*** Initialize Spring's web application context for the given servlet context,* using the application context provided at construction time, or creating a new one* according to the "{@link #CONTEXT_CLASS_PARAM contextClass}" and* "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params.* @param servletContext current servlet context* @return the new WebApplicationContext* @see #ContextLoader(WebApplicationContext)* @see #CONTEXT_CLASS_PARAM* @see #CONFIG_LOCATION_PARAM*/public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {    if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {        throw new IllegalStateException("Cannot initialize context because there is already a root application context present - " +"check whether you have multiple ContextLoader* definitions in your web.xml!");    }    Log logger = LogFactory.getLog(ContextLoader.class);    servletContext.log("Initializing Spring root WebApplicationContext");    if (logger.isInfoEnabled()) {        logger.info("Root WebApplicationContext: initialization started");    }    long startTime = System.currentTimeMillis();    try {        // Store context in local instance variable, to guarantee that        // it is available on ServletContext shutdown.        if (this.context == null) {            this.context = createWebApplicationContext(servletContext);        }        if (this.context instanceof ConfigurableWebApplicationContext) {            ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;            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 ->                    // determine parent for root web application context, if any.                    ApplicationContext parent = loadParentContext(servletContext);                    cwac.setParent(parent);                }                    configureAndRefreshWebApplicationContext(cwac, servletContext);            }        }            servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);        ClassLoader ccl = Thread.currentThread().getContextClassLoader();        if (ccl == ContextLoader.class.getClassLoader()){            currentContext = this.context;        }else if (ccl != null) {            currentContextPerThread.put(ccl, this.context);        }        if (logger.isDebugEnabled()) {                logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +   WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");        }        if (logger.isInfoEnabled()) {            long elapsedTime = System.currentTimeMillis() - startTime;            logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");        }        return this.context;    }catch (RuntimeException ex) {        logger.error("Context initialization failed", ex);        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);            throw ex;    }catch (Error err) {        logger.error("Context initialization failed", err);        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);            throw err;    }}
  1. DispatcherServlet初始化过程中initServletBean()也会创建一个WebApplicationContext对象,并将ContextLoaderListener创建的上下文对象作为父上下文,同样也会将其作为ServletContext的一个属性。
public abstract class FrameworkServlet extends HttpServletBean {    protected final void initServletBean() throws ServletException {        this.webApplicationContext = **initWebApplicationContext**();        **initFrameworkServlet**();    }    /**    * Initialize and publish the WebApplicationContext for this servlet.    * <p>Delegates to {@link #createWebApplicationContext} for actual creation    * of the context. Can be overridden in subclasses.    * @return the WebApplicationContext instance    * @see #FrameworkServlet(WebApplicationContext)    * @see #setContextClass    * @see #setContextConfigLocation    */    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;    }}
0 0