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)在处理请求时提供了事件机制,无论是否成功都会发布事件。
SpringMVC源码解析之DispatcherServlet:生命周期init()和destroy()_第1张图片

二、DispatcherServlet初始化

在上篇博客中已经提到,HttpServlet将初始化的逻辑委托给了无参init方法处理,下面我们从无参的init方法开始了解DispatcherServlet的初始化过程。

SpringMVC源码解析之DispatcherServlet:生命周期init()和destroy()_第2张图片

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 {

	@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 initializer : this.contextInitializers) {
		initializer.initialize(wac);
	}
}

根据contextInitializerClasses属性和配置文件中的globalInitializerClasses参数进行初始化得到contextInitializers,并调用其初始化initialize方法.。

public interface ApplicationContextInitializer {

	/**
	 * 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。

你可能感兴趣的:(SpringMVC)