一、SpringMVC的初始化流程

1. SpringMVC 源码简介

使用过spring开发web项目的小伙伴都知道,SpringMVC的核心类DispatcherServlet了.那么这篇文章,就作为笔者梳理的记录了;

2. DispatherServlet 的继承结构图

首先我们来看看DispatherServlet 的关系图


结构图

从图中可以看到DispatherServlet 有一个抽象父类FrameworkServlet,而FrameworkServlet还有一个抽象父类HttpServletBean;

我们先回忆一下,servlet实现的几种方式;

  • 实现 Servlet 接口
  • 继承 HttpServlet 类
  • 继承 GenericServlet类

既然通过上面的三种方式可以遵照Servlet的规范,创建自己的Servlet,那么Spring是怎么做的呢?

3. HttpServletBean

我们已经知道Spring的DispatcherServlet继承了FrameworkServlet, FrameworkServlet继承了HttpServletBean, 所以我们去看看HttpServletBean源码;以下是部分代码

@SuppressWarnings("serial")
public abstract class HttpServletBean extends HttpServlet implements EnvironmentCapable, EnvironmentAware {
    @Override
    public final void init() throws ServletException {
        if (logger.isDebugEnabled()) {
            logger.debug("Initializing servlet '" + getServletName() + "'");
        }
        // Set bean properties from init parameters.
        // 获取Servlet 的所有配置并转为 PropertyValues
        PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
        if (!pvs.isEmpty()) {
            try {
                //构造一个BeanWrapper;这个BeanWrapper的功能很简单,提供一个设置JavaBean属性的通用方法
                BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
                ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
                bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
                //初始化BeanWrapper , 空方法,在子类中实现。
                initBeanWrapper(bw);
                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.   继续完成 Servlet 的初始化操作。
        initServletBean();
        if (logger.isDebugEnabled()) {
            logger.debug("Servlet '" + getServletName() + "' configured successfully");
        }
    }
}

总结 : HttpServletBean做的事情很简单,总的说来就两个方面;同时都预留了空的方法给子类,加强了程序的扩展性;

  • 加载 Servlet 相关属性并设置给当前 Servlet 对象;
  • 然后调用 initServletBean() 方法继续完成 Servlet 的初始化操作;

4. FrameworkServlet

而initServletBean()方法的实现是在子类FrameworkServlet中,那我们就该去看看FrameworkServlet的源码了;
以下是FrameworkServlet中的部分代码,关键部分的操作,笔者已经在源码中标注出来了,方便阅读;

@SuppressWarnings("serial")
public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {

    @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 {
            //初始化webApplicationContext
            this.webApplicationContext = initWebApplicationContext();
            
            //初始化FrameworkServlet, 空方法
            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");
        }
    }

      protected WebApplicationContext initWebApplicationContext() {
               //根据ServletContext 获取WebApplicationContext 
               //Spring 会将WebApplicationContext 容器设置为 ServletContext 的一个属性,属性的 key 为 
               //org.springframework.web.context.WebApplicationContext.ROOT,所以根据这个 key 就可以调用 
               //ServletContext#getAttribute 方法获取到 rootContext 了
        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;
            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);
                    }
                    //配置和刷新容器,将this.refreshEventReceived置为true
                    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
            //去 ServletContext 中查找 WebApplicationContext 对象,找到了就赋值给 wac;
            wac = findWebApplicationContext();
        }
        if (wac == null) {
            // No context instance is defined for this servlet -> create a local one
            //创建一个 WebApplicationContext 对象并赋值给 wac;并配置和刷新容器将this.refreshEventReceived置为true
            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);
        }

        //将 wac 保存到到 ServletContext 中。保存的时候会根据 publishContext 变量的值来决定是否保存,
        //publishContext 可以在 web.xml 中配置 Servlet 时通过 init-param 进行配置,保存的目的是为了方便获取
        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;
    }
}

上面的这些步骤中,通过 createWebApplicationContext 方法创建 WebApplicationContext 对象需要和大家细说下,因为一般情况下就是通过这种方式创建的 WebApplicationContext。我们来看一下相关的方法:


    /**
     * 首先获取到创建类型,并检查创建类型,没问题的话调用 instantiateClass 方法完成创建工作,
     * 然后给创建好的 wac 对象配置各种属性,配置的 configLocation 
     * 就是我们在 web.xml 文件中配置的 SpringMVC 配置文件路径,
     * 默认的文件路径是 /WEB-INF/[servletName]-servlet.xml。
     */
    protected WebApplicationContext createWebApplicationContext(@Nullable 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);
        String configLocation = getContextConfigLocation();
        if (configLocation != null) {
            wac.setConfigLocation(configLocation);
        }
        configureAndRefreshWebApplicationContext(wac);

        return wac;
    }


    /**
     * configureAndRefreshWebApplicationContext 方法主要也是配置&刷新 WebApplicationContext,
     * 在这个方法里会调用 addApplicationListener 为 wac 添加一个监听器,监听的是 ContextRefreshedEvent 事件,
     * 当收到该事件后,会调用 FrameworkServlet 的 onApplicationEvent 方法,并在该方法中调用 onRefresh 方法完成刷新,
     * 刷新之后,会将 refreshEventReceived 变量标记为 true
     */
    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());
            }
        }

        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);
        wac.refresh();
    }

    //刷新WebApplicationContext,会将 refreshEventReceived 变量标记为 true
    public void onApplicationEvent(ContextRefreshedEvent event) {
      this.refreshEventReceived = true;
      synchronized (this.onRefreshMonitor) {
        onRefresh(event.getApplicationContext());//空方法,交由DispatherServlet实现
      }
    }

总结: 我们可以看到其实FrameworkServlet中主要做了一件事情,初始化webApplicationContext,调用initWebApplicationContext()方法,获取WebApplicationContext;而获取WebApplicationContext的情况分了很多种,具体的已经在上面的源码上给了注释;

那我们继续往下看, 在FrameworkServlet 中 的刷新操作实际是空方法,这是留给了子类DispatherServlet去实现的,现在终于到了我们熟知的DispatcherServlet了,下面就让我们具体看看,DispatcherServlet到底做了啥;

5. DispatcherServlet

直接上DispatcherServlet的源码, 具体步骤源码中已经添加了说明

九个组件的初始化流程比较类似,这里我们以常见的视图解析器的初始化方法 initViewResolvers 为例,来一起看看初始化流程:

    /**
     * 这是FrameworkServlet中的空方法, 由DispatcherServlet来实现
     * @param context
     */
    @Override
    protected void onRefresh(ApplicationContext context) {
        initStrategies(context); // 初始化策略
    }

    /**
     * 初始化策略, 初始化DispatcherServlet的九个组件
     * @param context
     */
    protected void initStrategies(ApplicationContext context) {
        initMultipartResolver(context);
        initLocaleResolver(context);
        initThemeResolver(context);
        initHandlerMappings(context);
        initHandlerAdapters(context);
        initHandlerExceptionResolvers(context);
        initRequestToViewNameTranslator(context);
        initViewResolvers(context);   //视图解析器
        initFlashMapManager(context);
    }

    /**
     * 初始化视图解析器,
     * @param context
     */
    private void initViewResolvers(ApplicationContext context) {
        this.viewResolvers = null;  //视图解析器集合 List viewResolvers

        if (this.detectAllViewResolvers) { //是否发现所有的视图解析器,默认为true
            // Find all ViewResolvers in the ApplicationContext, including ancestor contexts.
            // 通过ApplicationContext容器查询所有的视图解析器
            Map matchingBeans =
                    BeanFactoryUtils.beansOfTypeIncludingAncestors(context, ViewResolver.class, true, false);
            if (!matchingBeans.isEmpty()) {
                //将视图解析器放入集合并排序
                this.viewResolvers = new ArrayList<>(matchingBeans.values());
                // We keep ViewResolvers in sorted order.
                AnnotationAwareOrderComparator.sort(this.viewResolvers);
            }
        }
        else {
            //如果this.detectAllViewResolvers = false, 就从Spring中去寻找名为 "viewResolver" (VIEW_RESOLVER_BEAN_NAME)的视图解析器
            try {
                ViewResolver vr = context.getBean(VIEW_RESOLVER_BEAN_NAME, ViewResolver.class);
                this.viewResolvers = Collections.singletonList(vr);
            }
            catch (NoSuchBeanDefinitionException ex) {
                // Ignore, we'll add a default ViewResolver later.
            }
        }

        // Ensure we have at least one ViewResolver, by registering
        // a default ViewResolver if no other resolvers are found.
        // 如果从ApplicationContext 和 spring 容器中都没有找到与之对应的视图解析器,就采用默认的视图解析器
        if (this.viewResolvers == null) {
            this.viewResolvers = getDefaultStrategies(context, ViewResolver.class);
            if (logger.isDebugEnabled()) {
                logger.debug("No ViewResolvers found in servlet '" + getServletName() + "': using default");
            }
        }
    }

    /**
     * 获取默认的策略
     * @param context
     * @param strategyInterface
     * @param 
     * @desc 默认实现使用“DispatcherServlet.properties文件,
     *       文件(与DispatcherServlet类在同一个包中)来确定类名。
     *       它通过上下文的BeanFactory实例化strategy对象。
     * @return
     */
    @SuppressWarnings("unchecked")
    protected  List getDefaultStrategies(ApplicationContext context, Class strategyInterface) {
        String key = strategyInterface.getName();
        String value = defaultStrategies.getProperty(key);
        if (value != null) {
            String[] classNames = StringUtils.commaDelimitedListToStringArray(value);
            List strategies = new ArrayList<>(classNames.length);
            for (String className : classNames) {
                try {
                    Class clazz = ClassUtils.forName(className, DispatcherServlet.class.getClassLoader());
                    Object strategy = createDefaultStrategy(context, clazz);
                    strategies.add((T) strategy);
                }
                catch (ClassNotFoundException ex) {
                    throw new BeanInitializationException(
                            "Could not find DispatcherServlet's default strategy class [" + className +
                                    "] for interface [" + key + "]", ex);
                }
                catch (LinkageError err) {
                    throw new BeanInitializationException(
                            "Unresolvable class definition for DispatcherServlet's default strategy class [" +
                                    className + "] for interface [" + key + "]", err);
                }
            }
            return strategies;
        }
        else {
            return new LinkedList<>();
        }
    }

从源码可以发现,DispatcherServlet的初始化默认策略是通过反射去加载了一个和DispatcherServlet同级包下的 DispatcherServlet.properties 文件, 以下是XML中的内容

# 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

从DispatcherServlet.properties文件中可以发现:默认的视图解析器是InternalResourceViewResolver;

其次还可以看到,这里一共定义了 8 个默认的键值对,有的值是一个,有的值是多个。前面 initStrategies 方法中一共要初始化九个组件,这里默认只定义了 8 个,少了一个 MultipartResolver,这也好理解,并非所有的项目都有文件上传,而且即使有文件上传,用哪一个具体的 MultipartResolver 也不好确定,还是要开发者自己决定。

以上是 initViewResolvers 的工作流程,另外 8 个也和它差不多,唯一不同的是 initMultipartResolver,如下:

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

可以看到,它只是根据 bean 的名字去查找 bean 实例,没有去查找默认的 MultipartResolver。

6. 总结

以上就是SpringMVC 的初始化流程,主要涉及到了 HttpServletBean、FrameworkServlet 以及 DispatcherServlet 三个实例;
HttpServletBean 主要是加载 Servlet 配置的各种属性并设置到 Servlet 上;
FrameworkServlet 则主要是初始化了 WebApplicationContext;
DispatcherServlet 则主要是初始化了自身的九个组件。

本篇文章,个人梳理源码的同时,还引用到了大佬 _江南一点雨 的文章, 目的是用于个人学习的记录,原文出处https://blog.csdn.net/u012702547/article/details/114820956?spm=1001.2014.3001.5501

你可能感兴趣的:(一、SpringMVC的初始化流程)