SpringMVC源碼解析之DispatcherServlet:生命週期init()和destroy()

SpringMVC源碼解析之Servlet
SpringMVC源碼解析之GenericServlet和HttpServlet

一、DispatcherServlet類

DispatcherServlet是SpringMVC的基礎Servlet,基於Java-bean提供的應用上下文(application context)的集成。
Dispatcher主要提供了以下功能:
(1)通過Servlet提供了對WebApplicationContext實例的管理。Servlet的配置由Servlet命名空間內的beans確定。
(2)在處理請求時提供了事件機制,無論是否成功都會發布事件。
DispatcherServlet類圖

二、DispatcherServlet初始化

在上篇博客中已經提到,HttpServlet將初始化的邏輯委託給了無參init方法處理,下面我們從無參的init方法開始瞭解DispatcherServlet的初始化過程。

DispatcherServlet 初始化

1. HttpServletBean#init()

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

	//配置的參數封裝到pvs
	// Set bean properties from init parameters.
	PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
	if (!pvs.isEmpty()) {
		try {
			//Spring中bean的包裝工具類,用於直接修改bean對象的屬性值
			BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
			ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
			bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
			//空的模板方法,留給子類重寫
			initBeanWrapper(bw);
			//根據配置的參數值修改Servlet屬性
			bw.setPropertyValues(pvs, true);
		}
		catch (BeansException ex) {
			if (logger.isErrorEnabled()) {
				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");
	}
}

2. FrameworkServlet#initServletBean()

FrameServlet對initServletBean方法進行了重寫。

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 {
		//初始化成員變量webApplicationContext
		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");
	}
}

可以看到,除了日誌記錄初始化時間外,主要功能是初始化webApplicationContext。

3. FrameworkServlet#initWebApplicationContext()

protected WebApplicationContext initWebApplicationContext() {
	//獲取根容器rootContext。Spring默認會將根容器設置爲ServletContext的屬性,
	//默認key值爲:String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
	WebApplicationContext rootContext =
			WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	WebApplicationContext wac = null;

	//webApplicationContext已經在構造方法中被注入
	if (this.webApplicationContext != null) {
		// A context instance was injected at construction time -> use it
		wac = this.webApplicationContext;
		//wac instanceof ConfigurableWebApplicationContext類型需要保證active
		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
		//webApplicationContext已經在構造方法中沒有被注入,從ServletContext中查找
		wac = findWebApplicationContext();
	}
	if (wac == null) {
		// No context instance is defined for this servlet -> create a local one
		//ServletContext中也沒有找到,進行創建
		wac = createWebApplicationContext(rootContext);
	}
	
	//刷新事件沒有出發時,進行刷新。刷新方法也是默認空的模板方法。確保onRefresh至少被調用過一次。
	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);
	}

	//作爲屬性註冊到servletContext(註冊之後與findWebApplicationContext對應)
	//默認爲true
	if (this.publishContext) {
		// Publish the context as a servlet context attribute.
		//前綴(SERVLET_CONTEXT_PREFIX = FrameworkServlet.class.getName() + ".CONTEXT.") + servletName
		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;
}

(1)獲取根容器rootContext
(2)獲取或生成容器webApplicationContext並設置, 其保證至少執行了一次onRefresh方法
(2.1)webApplicationContext已經在構造方法中傳入
(2.2)webApplicationContext已經注入到servletContext,根據getServletContextAttributeName進行查找
(2.3)以根容器作爲父容器通過createWebApplicationContext進行生成
(3)如果需要,將webApplicationContext註冊到servletContext。
這一步可以和(2.2)對應。

4.FrameworkServlet#createWebApplicationContext(WebApplicationContext)

protected WebApplicationContext createWebApplicationContext(@Nullable ApplicationContext parent) {
	//WebApplicationContext的class類型,默認爲XmlWebApplicationContext
	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 + "]");
	}
	//檢驗,contextClass應該是ConfigurableWebApplicationContext或其子類
	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);
	String configLocation = getContextConfigLocation();
	if (configLocation != null) {
		wac.setConfigLocation(configLocation);
	}
	//配置並刷新
	configureAndRefreshWebApplicationContext(wac);

	return wac;
}

根據父容器創新新的容器。
首先獲取容器的class類型,默認爲XmlWebApplicationContext。
接着進行校驗,class類型必須屬於ConfigurableWebApplicationContext。
然後初始化並設置屬性。
最後進行配置和刷新。

5.FrameworkServlet#configureAndRefreshApplicationContext(ConfigurableApplicationContext)

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
	//設置id屬性
	if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
		// The application context id is still set to its original default value
		// -> assign a more useful id based on available information
		if (this.contextId != null) {
			wac.setId(this.contextId);
		}
		else {
			// Generate default id...
			wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
					ObjectUtils.getDisplayString(getServletContext().getContextPath()) + '/' + getServletName());
		}
	}

	wac.setServletContext(getServletContext());
	wac.setServletConfig(getServletConfig());
	wac.setNamespace(getNamespace());
	//添加監聽器SourceFilteringListener
	wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));

	// The wac environment's #initPropertySources will be called in any case when the context
	// is refreshed; do it eagerly here to ensure servlet property sources are in place for
	// use in any post-processing or initialization that occurs below prior to #refresh
	ConfigurableEnvironment env = wac.getEnvironment();
	if (env instanceof ConfigurableWebEnvironment) {
		((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());
	}

	//默認空的模板方法
	postProcessWebApplicationContext(wac);
	applyInitializers(wac);
	wac.refresh();
}

可以看到此處給ApplicationContext添加了監聽器SourceFilteringListener,,而SourceFilteringListenerh使用了委派模式,實際使用的是ContextRefreshListener的監聽邏輯。
ContextRefreshListener是FrameworkServlet的內部類,監聽刷新事件ContextRefreshedEvent。

private class ContextRefreshListener implements ApplicationListener<ContextRefreshedEvent> {

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		FrameworkServlet.this.onApplicationEvent(event);
	}
}

//修改refreshEventReceived標誌並調用onRefresh方法,
//FrameworkServlet#initWebApplication()方法中根據refreshEventReceived標誌判斷是否調用onRefresh對應
public void onApplicationEvent(ContextRefreshedEvent event) {
	this.refreshEventReceived = true;
	onRefresh(event.getApplicationContext());
}

回到FrameworkServlet#configureAndRefreshApplicationContext(ConfigurableApplicationContext)可以看到最後其進行了一次刷新,以觸發onRefresh方法。
再回到FrameworkServlet#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);
}

可以看到,如果沒有執行過onRefresh方法,則觸發執行,確保至少執行了一次。

6. FrameworkServlet#applyInitializers(ConfigurableApplicationContext)

protected void applyInitializers(ConfigurableApplicationContext wac) {
	//獲取配置文件中的globalInitializerClasses參數
	String globalClassNames = getServletContext().getInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM);
	if (globalClassNames != null) {
		for (String className : StringUtils.tokenizeToStringArray(globalClassNames, INIT_PARAM_DELIMITERS)) {
			this.contextInitializers.add(loadInitializer(className, wac));
		}
	}

	if (this.contextInitializerClasses != null) {
		for (String className : StringUtils.tokenizeToStringArray(this.contextInitializerClasses, INIT_PARAM_DELIMITERS)) {
			this.contextInitializers.add(loadInitializer(className, wac));
		}
	}

	AnnotationAwareOrderComparator.sort(this.contextInitializers);
	for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : this.contextInitializers) {
		initializer.initialize(wac);
	}
}

根據contextInitializerClasses屬性和配置文件中的globalInitializerClasses參數進行初始化得到contextInitializers,並調用其初始化initialize方法.。

public interface ApplicationContextInitializer<C extends ConfigurableApplicationContext> {

	/**
	 * Initialize the given application context.
	 * @param applicationContext the application to configure
	 */
	void initialize(C applicationContext);
}

ApplicationContextInitializer是ConfigurableApplicationContext初始化的回調接口,用來做一些初始化動作。

7. DispatcherServlet#onRefresh(ApplicationContext)

DispatcherServlet對onRefresh進行了重寫,用於在ApplicationContext刷新後進行策略組件的初始化。

DispatcherServlet#onRefresh(ApplicationContext)
protected void onRefresh(ApplicationContext context) {
	initStrategies(context);
}
//初始化9個策略組件
protected void initStrategies(ApplicationContext context) {
	initMultipartResolver(context);
	initLocaleResolver(context);
	initThemeResolver(context);
	initHandlerMappings(context);
	initHandlerAdapters(context);
	initHandlerExceptionResolvers(context);
	initRequestToViewNameTranslator(context);
	initViewResolvers(context);
	initFlashMapManager(context);
}

private void initMultipartResolver(ApplicationContext context) {
	try {
		this.multipartResolver = context.getBean(MULTIPART_RESOLVER_BEAN_NAME, MultipartResolver.class);
		if (logger.isDebugEnabled()) {
			logger.debug("Using MultipartResolver [" + this.multipartResolver + "]");
		}
	}
	catch (NoSuchBeanDefinitionException ex) {
		// Default is no multipart resolver.
		this.multipartResolver = null;
		if (logger.isDebugEnabled()) {
			logger.debug("Unable to locate MultipartResolver with name '" + MULTIPART_RESOLVER_BEAN_NAME +
					"': no multipart request handling provided");
		}
	}
}

以DispatcherServlet#initMultipartResolver爲例,用於確定成員變量multipartResolver的值。
其它8個方法也類似。
這一點和前面對DispatcherServlet的介紹相吻合。
除了MultipartResolver外,其它的8個組件都指定了默認值,默認值的類型可以查看DispatcherServlet.properties。

# Default implementation classes for DispatcherServlet's strategy interfaces.
# Used as fallback when no matching beans are found in the DispatcherServlet context.
# Not meant to be customized by application developers.

org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver

org.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolver

org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping

org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter

org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver,\
	org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\
	org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver

org.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator

org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver

org.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager

通過Servlet提供了對WebApplicationContext實例的管理。Servlet的配置由Servlet命名空間內的beans確定。

三、DispatcherServlet銷燬

1. FrameworkServlet#destroy()

public void destroy() {
	getServletContext().log("Destroying Spring FrameworkServlet '" + getServletName() + "'");
	// Only call close() on WebApplicationContext if locally managed...
	if (this.webApplicationContext instanceof ConfigurableApplicationContext && !this.webApplicationContextInjected) {
		((ConfigurableApplicationContext) this.webApplicationContext).close();
	}
}

銷燬邏輯很簡單,只需要打印日誌和關閉webApplicationContext。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章