前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringMVC之细说DispatcherServlet

SpringMVC之细说DispatcherServlet

作者头像
一觉睡到小时候
发布2019-07-02 17:10:31
3.4K0
发布2019-07-02 17:10:31
举报
文章被收录于专栏:国产程序员国产程序员

DispatcherServlet的启动以及初始化

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

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

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

DispatcherServlet类继承体系

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

1、DispatcherServlet的启动和初始化

DispatcherServlet启动

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

代码语言:javascript
复制
@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()方法如下:

代码语言:javascript
复制
@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()函数:

代码语言:javascript
复制
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()函数如下:

代码语言:javascript
复制
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()函数:

代码语言:javascript
复制
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类中重写,代码如下:

代码语言:javascript
复制
@Override
protected void onRefresh(ApplicationContext context) {
//调用initStrategies完成MVC模块初始化
        initStrategies(context);
        }

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

protected void initStrategies(ApplicationContextcontext) { initMultipartResolver(context);//初始化文件上传解析器 initLocaleResolver(context);//初始化本地解析器 initThemeResolver(context);//初始化主题解析器 initHandlerMappings(context);//初始化处理器映射器 initHandlerAdapters(context);//初始化处理器适配器 initHandlerExceptionResolvers(context);//初始化异常解析器 initRequestToViewNameTranslator(context);//初始化请求到视图名称解析器 initViewResolvers(context);//初始化视图解析器 initFlashMapManager(context); }

DispatcherServlet.properties的默认配置:

代码语言:javascript
复制
# 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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-06-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 国产程序员 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档