Spring MVC DispatcherServlet的启动以及初始化

Spring MVC是一个MVC模式的实现,在使用Spring MVC 时,主要需要在web.xml配置文件中设置DispatcherServlet,这个Servlet是实现Spring mvc 的前端控制器,所有的Web请求都需要通过它来处理,进行匹配、转发、数据处理。DispatcherServlet是实现Spring MVC最核心的部分。

在使用SpringMVC 时我们通常需要如下配置:

<servlet>
   <servlet-name>mvc-dispatcher</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:springmvc.xml</param-value>
        </init-param>
       <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
   <servlet-name>mvc-dispatcher</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>

Web容器启动,并开始加载各个Servlet,DispatcherServlet将建立自己的上下文来持有Spring MVC的bean对象,在建立这个自己持有的IoC容器时,会从ServletContext中得到根上下文作为DispatcherServlet持有的上下午的双亲上下文。有了这个根上下文,在对自己持有的上下文进行初始化,从而完成SpringMVC运行环境。在配置DispatcherServlet时可以设置初始化参数指定Ioc容器bean定义配置文件,名:<param-name>contextConfigLocation</param-name>,值:<param-value>classpath:springmvc.xml</param-value>,如果不指定,默认使用/WEB-INF/DispatcherServlet名-servlet.xml。上述配置如不指定 <init-param>,将使用/WEB-INF/mvc-dispatcher-servlet.xml。

1、DispatcherServlet类继承体系

DispatcherServlet通过继承FrameworkServlet和HttpServletBean而继承了HttpServlet,通过使用ServletAPI来对HTTP请求进行处理成为了Spring MVC的前端处理器,同时也实现了MVC模块与WEB容器集成。

2、DispatcherServlet的启动和初始化

DispatcherServlet启动

首先作为一个Servlet,在其生命周期中,将调用init()方法,完成初始化相关工作,init()方法在HttpServletBean中进行了重写,代码如下:


@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;
}
//这里调用子类FrameWorkServlet中重写的initServletBean()方法继续进行初始化
// Let subclasses do whatever initialization they like.
initServletBean();

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

在FrameWorkServlet类中,initServletBean()方法如下:

@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 {
//这里通过调用initWebApplicationContext(),初始化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");
}
}

继续看initWebApplicationContext()函数:


protected WebApplicationContext initWebApplicationContext() {
  //这里通过WebApplicationContextUtils工具类来获取根web应用上下文,这个上下文就是之前提到的全局应用根上下文,保存在ServletContext中,这个根上下文将作为当前mvc servlet上下文的双亲上下文。
   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) {
    //这里真正创建需要与该Servlet相关联的WebApplicationContext
// 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.
//这里,ioc容器已经创建初始化完成,执行onRefresh(wac)进一步初始化MVC其他模块,后面说明
onRefresh(wac);
}

if (this.publishContext) {
    //这里将当前建立的上下文保存到ServletContext中,attrName 是与当前servlet名相关联,保证唯一性
// 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;
}

下面来看这个当前上下文是如何创建的,createWebApplicationContext()函数如下:


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");
}
  //通过BeanUtils反射实例化具体的上下文对象,contextClass通过getContextClass()获取,取得的class是Class<?> DEFAULT_CONTEXT_CLASS = XmlWebApplicationContext.class;
   ConfigurableWebApplicationContext wac =
         (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
 //对上下文对象进行设置
wac.setEnvironment(getEnvironment());
wac.setParent(parent);
wac.setConfigLocation(getContextConfigLocation());
//这里对刚刚创建的WebApplicationContext,进行配置和初始化操作,完成容器的最终初始化
configureAndRefreshWebApplicationContext(wac);

   return wac;
}

继续configureAndRefreshWebApplicationContext()函数:

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
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());
}
   }
 //设置ServletContext引用,以及其他对象的设置
   wac.setServletContext(getServletContext());
wac.setServletConfig(getServletConfig());
wac.setNamespace(getNamespace());
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);
//嗲用容器的refresh()方法,完成日期初始化
wac.refresh();
}

可以看到对上下文进行了相关配置,设置ServletContext,设置初始化参数等,最后通过调用容器的refresh()方法启动整个容器,就像一般的IoC容器初始化过程一样。

至此,web应用上下文的启动、初始化已经完成,DispatcherServlet运行MVC需要的IoC容器已经建立起来,但是DispatcherServlet作为Spring
MVC前端控制器,还需要初始化MVC需要的其他模块,回到initWebApplicationContext()函数,看到在wac
= createWebApplicationContext(rootContext)后面,调用了onRefresh(was)函数,onRefresh函数在DispatcherServlet类中重写,代码如下:


@Override
protected void onRefresh(ApplicationContext context) {
//调用initStrategies完成MVC模块初始化
   initStrategies(context);
}

可以看到MVC初始化是在DispatcherServlet的initStrategies()方法中完成,在该方法中初始化各种MVC框架实现元素,如至此request映射的HandlerMappings,以及handler适配处理器HandlerAdapters,以及视图生成器ViewResolver等。


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

比如初始化HandlerMappings,在SpringMVC中,HandlerMappings的作用是为Http请求找到对应的Controller控制器,来进一步处理请求。


private void initHandlerMappings(ApplicationContext context) {
this.handlerMappings = null;
 //这里根据detectAllHandlerMappings布尔值确定是否导入所有的handlerMappings ,默认导入所有handlerMappings bean,这些bean可以来自于当前上下文容器,也可以来自其双亲根web上下文容器
   if (this.detectAllHandlerMappings) {
// Find all HandlerMappings in the ApplicationContext, including ancestor contexts.
Map<String, HandlerMapping> matchingBeans =
            BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
      if (!matchingBeans.isEmpty()) {
this.handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values());
// We keep HandlerMappings in sorted order.
OrderComparator.sort(this.handlerMappings);
}
   }
else {
try {
         HandlerMapping hm = context.getBean(HANDLER_MAPPING_BEAN_NAME, HandlerMapping.class);
         this.handlerMappings = Collections.singletonList(hm);
}
catch (NoSuchBeanDefinitionException ex) {
// Ignore, we'll add a default HandlerMapping later.
}
   }
//如果没有发现配置的handlerMappings ,需要设置默认的handlerMappings ,这些默认的handlerMappings 通过配置文件,在jar包中指定,在DispatcherServlet.properties配置文件中默认设置BeanNameUrlHandlerMapping、DefaultAnnotationHandlerMapping
// Ensure we have at least one HandlerMapping, by registering
   // a default HandlerMapping if no other mappings are found.
if (this.handlerMappings == null) {
this.handlerMappings = getDefaultStrategies(context, HandlerMapping.class);
      if (logger.isDebugEnabled()) {
logger.debug("No HandlerMappings found in servlet '" + getServletName() + "': using default");
}
   }
}

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.annotation.DefaultAnnotationHandlerMapping

org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,   org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,   org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,   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
时间: 2024-10-04 00:33:43

Spring MVC DispatcherServlet的启动以及初始化的相关文章

spring mvc web应用启动时就执行特定处理(线程启动)

package com.sdt.platform.index.controller; import java.net.URL; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.annotation.Resource; import org.springframework.beans.BeansException; import org.sprin

spring mvc DispatcherServlet详解之一---前端控制器架构

前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端.前端控制器既可以使用Filter实现(Struts2采用这种方式),也可以使用Servlet来实现(spring MVC框架). DispatcherServlet 作为前置控制器是web服务器的入口,是spring mvc最重要的一个类,通过它的生命周期可以加深对web服务器的理解. servlet的生命周期 首先我们回忆一下ser

spring mvc DispatcherServlet详解之前传---FrameworkServlet

做项目时碰到Controller不能使用aop进行拦截,从网上搜索得知:使用spring mvc 启动了两个context:applicationContext 和WebapplicationContext. 首先我们来了解applicationContext 和WebapplicationContext区别和联系吧 1. ApplicationContext和WebApplicationContext是继承关系 /** * Interface to provide configuration

spring mvc DispatcherServlet详解之---获取控制器

整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的第一步:获取控制器. HandlerMapping HandlerMappings 定义request和handler之间的映射.它的官方文档这样描述: Interface HandlerMapping All Known Implementing Classes: AbstractControllerUrlHandlerMapping, AbstractDetecting

spring mvc DispatcherServlet详解之interceptor和filter的区别

首先我们看一下spring mvc Interceptor的功能及实现: http://wenku.baidu.com/link?url=Mw3GaUhCRMhUFjU8iIDhObQpDcbmmRy_IPeumazg0ppnbmwqFUtLp9kSpuPPpeysf6EnHBLYFeWrbjqMq8BlWKQz_7MSDhGQTVl32fpxCMm SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理,其他的作用比如通过它

spring mvc DispatcherServlet详解之一---处理请求深入解析

要深入理解spring mvc的工作流程,就需要先了解spring mvc的架构: 从上图可以看到 前端控制器DispatcherServlet在其中起着主导作用,理解了DispatcherServlet 就完全可以说弄清楚了spring mvc. 为了加深对spring mvc的整个工作流程的理解,本文从分析DispatcherServlet的工作过程来一窥spring mvc的整个面貌. 1. 初始化 protected void initStrategies(ApplicationCont

spring mvc零配置启动

简单实现一个不配置xml文件的spring mvc项目 参考https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html 项目结构 MyWebApplicationInitializer.class /** * 关键点,实现WebApplicationInitializer这个接口可以实现在tomcat初始化时调用其onStartup方法 */ public class MyWebApplicatio

spring mvc DispatcherServlet详解之三---request通过ModelAndView中获取View实例的过程

整个spring mvc的架构如下图所示: 上篇文件讲解了DispatcherServlet第二步:通过request从Controller获取ModelAndView.现在来讲解第三步:request 从ModelAndView中获取view对象. 获取view对象一般是通过viewResolver来解析view name来完成的.若ModelAndView中view 不存在或者ModelAndView本身为null则填充默认值.代码如下: ModelAndView中view 不存在或者Mod

spring mvc DispatcherServlet详解之---视图渲染过程

整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的最后一步:视图渲染.视图渲染的过程是在获取到ModelAndView后的过程. 视图渲染的过程: DispatcherServlet.java doService()--->doDispatch()--->processDispatchResult()--->render() processDispatchResult():主要处理异常.请求状态及触发请求完成事件,