Spring之SpringMVC(源碼)啓動初始化過程分析

1.說明

    SpringMVC做爲Spring提供的MVC實現,能夠實現與Spring的自然無縫聯合,由於具備很普遍的用途。具體的關於SpringMVC的處理流程邏輯我在這裏就不在贅述了。仍是來經過源碼來追述下SpringMVC的啓動過程。前端

2.入口

DispatcherServlet做爲SpringMVC的前端控制器,具備很核心的地位。來看下它的繼承結構。java

能夠看到DispatcherServlet依次繼承了GenericServlet、HttpServlet、HttpServletBean、FrameworkServlet.因爲DispatcherServlet是繼承了HttpServlet,因此它的初始化入口應該是HttpServlet的init()方法,Web容器啓動時將調用它的init方法init()方法具體作了什麼工做。web

	public final void init() throws ServletException {
		if (logger.isDebugEnabled()) {
			logger.debug("Initializing servlet '" + getServletName() + "'");
		}

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

  注意這個方法是final,不可以被覆蓋,它位於HttpServletBean中。它完成的功能有兩個,第一個將將Servlet初始化參數設置到該Servlet中,第二個調用子類的初始化。app

3.HttpServletBean的 initServletBean()

從上面可知,initServletBean方法主要用於子類的處理話過程。看下HttpServletBean的子類FrameworkServlet.看下具體的實現:ide

	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 {
			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是SpringMVC的一個基礎Servlet,經過它能夠完成和Spring的整合。經過initServletBean()的代碼,可知只要操做有兩個initWebApplicationContext();和initFrameworkServlet();第一個完成了Web上下文的初始化工做:ContextLoaderListener 加載了上下文將做爲根上下文(DispatcherServlet 的父容器),第二個則提供給子類進行初始化的擴展點:行容器的一些初始化,這個方法由子類實現,來進行擴展。。工具

咱們來看下它是如何完成Web上下文的初始化工做 initWebApplicationContext(); 實現代碼:ui

	protected WebApplicationContext initWebApplicationContext() {
		WebApplicationContext rootContext =
				WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		WebApplicationContext wac = null;

		if (this.webApplicationContext != null) {
			//在建立的時候注入根上下文
			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;
	}

  它首先經過Spring提供的工具類 WebApplicationContextUtils 獲取Spring 的根上下文(ContextLoaderListener加載的)。主要操做有1.在建立該Servlet的時候注入根上下文,2.若是上下文爲空,那麼就查找已經綁定的上下文,以下所示this

	protected WebApplicationContext findWebApplicationContext() {
		String attrName = getContextAttribute();
		if (attrName == null) {
			return null;
		}
		WebApplicationContext wac =
				WebApplicationContextUtils.getWebApplicationContext(getServletContext(), attrName);
		if (wac == null) {
			throw new IllegalStateException("No WebApplicationContext found: initializer not registered?");
		}
		return wac;
	}

  第三個操做,若是沒有找到相應的上下文,並指定父親爲ContextLoaderListener,手動建立一個spa

	protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
		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;
	}

  第四步,無論這個上下文是否是ConfigurableApplicationContext或者在構建的時候刷新過,都須要從新刷新上下文,完成一些初始化的工做。debug

第四步的實現是放到DispatcherServlet中的onRefresh實現的,具體來看代碼,

	@Override
	protected void onRefresh(ApplicationContext context) {
		initStrategies(context);
	}

	protected void initStrategies(ApplicationContext context) {
		initMultipartResolver(context);
		initLocaleResolver(context);
		initThemeResolver(context);
		initHandlerMappings(context);
		initHandlerAdapters(context);
		initHandlerExceptionResolvers(context);
		initRequestToViewNameTranslator(context);
		initViewResolvers(context);
		initFlashMapManager(context);
	}

  

它主要完成了控制器相關的配置工做,具體工做。。。。好多。。暫緩。

4.總結

SpringMVC初始化啓動過程當中作的事情比較簡單,初始化 Spring Web MVC 使用的 Web 上下文而且指定ContextLoaderListener爲父容器;還有就是上面代碼所示的那樣初始DispatcherServlet 使用的策略。

 

未完待續!!!!!!!!

相關文章
相關標籤/搜索