SpringMVC源碼解析-DispatcherServlet啓動流程和初始化

   在使用springmvc框架,會在web.xml文件配置一個DispatcherServlet,這正是web容器開始初始化,同時會在創建本身的上下文來持有SpringMVC的bean對象。前端

先從DispatcherServlet入手,從名字來看,它是一個Servlet。它的定義以下:java

public class DispatcherServlet extends FrameworkServlet {

    它是繼承FrameworkServlet,來看一下整個的繼承關係。web

從繼承關係來看,DispatcherServlet繼承FrameworkServlet和HttpServletBean而繼承HttpServlet,經過使用Servlet API 來對HTTP請求進行響應,成爲SpringMVC的前端處理器。spring

先看一個時序圖mvc

注:做爲Servlet,DispatcherServlet的啓動和Servlet的啓動相關聯的。在Servlet初始化過程當中,Servlet的init方法會被調用,以進行初始化,然而DispatcherServlet的基類,因此從HttpServletBean中的初始化過程開始。      app

        DispatcherServlet的工做分爲2部分,一部分是初始化(也就是圖的上半部分),有initServletBean()啓動,經過initWebApplicationContext()方法最終調用DispatcherServlet中的initStrategies()方法。另外一部分(也就是圖的下半部分),是對HTTP請求進行響應,做爲Servlet,Web容器會調用Servlet的doGet()和doPost()方法,在通過FrameworkServlet的processRequest()簡單處理後,會調用DispatcherServlet的doService方法,在這個方法調用中封裝了doDispatch(),繼續調用processDispatchResult方法返回調用信息。框架

接下來看看初始化的源碼的流程:ide

HttpServletBean.init方法ui

/**
	 * Map config parameters onto bean properties of this servlet, and
	 * invoke subclass initialization.
	 * @throws ServletException if bean properties are invalid (or required
	 * properties are missing), or if subclass initialization fails.
	 */
	@Override
	public final void init() throws ServletException {
		if (logger.isDebugEnabled()) {
			logger.debug("Initializing servlet '" + getServletName() + "'");
		}
          //獲取Servlet初始化參數
		// Set bean properties from init parameters.
		try {
			PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
			BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
			ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
			bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
			initBeanWrapper(bw);
			bw.setPropertyValues(pvs, true);
		}
		catch (BeansException ex) {
			logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
			throw ex;
		}
                //調用子類的方法
		// Let subclasses do whatever initialization they like.
		initServletBean();

		if (logger.isDebugEnabled()) {
			logger.debug("Servlet '" + getServletName() + "' configured successfully");
		}
	}

  例如web.xml中配置參數:this

<servlet>  
    <servlet-name>TestServlet</servlet-name>  
    <servlet-class>com.lzyer.TestServlet</servlet-class>  
    <!--配置參數,能夠經過ServletConfig獲取參數-->
    <init-param>  
        <param-name>servlet-name</param-name>  
        <param-value>TestServlet</param-value>  
    </init-param>  
 </servlet>  
 <servlet-mapping>  
    <servlet-name>TestServlet</servlet-name>  
    <url-pattern>/TestServlet</url-pattern>  
  </servlet-mapping>

  FrameworkServlet.initServletBean方法

/**
	 * Overridden method of {@link HttpServletBean}, invoked after any bean properties
	 * have been set. Creates this servlet's WebApplicationContext.
	 */
	@Override
	protected final void initServletBean() throws ServletException {
		getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
		if (this.logger.isInfoEnabled()) {
			this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
		}
		long startTime = System.currentTimeMillis();

		try {
                         //建立Web應用上下文
			this.webApplicationContext = initWebApplicationContext();
			initFrameworkServlet();
		}
		catch (ServletException ex) {
			this.logger.error("Context initialization failed", ex);
			throw ex;
		}
		catch (RuntimeException ex) {
			this.logger.error("Context initialization failed", ex);
			throw ex;
		}

		if (this.logger.isInfoEnabled()) {
			long elapsedTime = System.currentTimeMillis() - startTime;
			this.logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +
					elapsedTime + " ms");
		}
	}

  FrameworkServlet.initWebApplicationContext()

/**
	 * 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;
	}

  先看WebApplicationContextUtils.getWebApplicationContext(getServletContext()),,主要是從ServletContext獲取WebApplicationContext

public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName) {
		Assert.notNull(sc, "ServletContext must not be null");
//從ServletContext中獲取 Object attr = sc.getAttribute(attrName); if (attr == null) { return null; } if (attr instanceof RuntimeException) { throw (RuntimeException) attr; } if (attr instanceof Error) { throw (Error) attr; } if (attr instanceof Exception) { throw new IllegalStateException((Exception) attr); } if (!(attr instanceof WebApplicationContext)) { throw new IllegalStateException("Context attribute is not of type WebApplicationContext: " + attr); } return (WebApplicationContext) attr; }

 findWebApplicationContext和上面同樣從ContextServlet中查找,若是不存在就調用下面的createWebApplicationContext方法。

protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
          //獲取contextClass Class<?> contextClass = getContextClass(); if (this.logger.isDebugEnabled()) { this.logger.debug("Servlet with name '" + getServletName() + "' will try to create custom WebApplicationContext context of class '" + contextClass.getName() + "'" + ", using parent context [" + parent + "]"); } if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException( "Fatal initialization error in servlet with name '" + getServletName() + "': custom WebApplicationContext class [" + contextClass.getName() + "] is not of type ConfigurableWebApplicationContext"); }
//經過反射獲取實例 ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); wac.setEnvironment(getEnvironment());
//設置雙親上下文 wac.setParent(parent); wac.setConfigLocation(getContextConfigLocation()); //配置和刷新應用 configureAndRefreshWebApplicationContext(wac); return wac; }

  看一下getContextClass()究竟是哪一個類? XmlWebApplicationContext.class

最後configureAndRefreshWebApplicationContext調用refresh方法啓動容器。

回到initWebApplicationContext方法中

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);
		}

  這個會觸發SpringMVC初始化策略

/**
	 * This implementation calls {@link #initStrategies}.
	 */
	@Override
	protected void onRefresh(ApplicationContext context) {
		initStrategies(context);
	}

	/**
	 * Initialize the strategy objects that this servlet uses.
	 * <p>May be overridden in subclasses in order to initialize further strategy objects.
	 */
	protected void initStrategies(ApplicationContext context) {
		initMultipartResolver(context);
		initLocaleResolver(context);
		initThemeResolver(context);
//映射關係 initHandlerMappings(context); initHandlerAdapters(context); initHandlerExceptionResolvers(context); initRequestToViewNameTranslator(context); initViewResolvers(context); initFlashMapManager(context); }

  到此,SpringMVC的初始化的流程大概就是這樣,下篇就是SpringMVC請求流程。

相關文章
相關標籤/搜索