likes
comments
collection
share

SpringMVC源码解析-0xml配置原理

作者站长头像
站长
· 阅读数 14

前言

看一段官网原话:

Spring MVC, as many other web frameworks, is designed around the front controller pattern where a central Servlet, the DispatcherServlet, provides a shared algorithm for request processing, while actual work is performed by configurable delegate components. This model is flexible and supports diverse workflows. 翻译:Spring MVC 与许多其他 Web 框架一样,围绕前端控制器模式设计,其中中央 Servlet DispatcherServlet 为请求处理提供共享算法,而实际工作由可配置的委托组件执行。 该模型非常灵活,支持多种工作流程。

The DispatcherServlet, as any Servlet, needs to be declared and mapped according to the Servlet specification by using Java configuration or in web.xml. In turn, the DispatcherServlet uses Spring configuration to discover the delegate components it needs for request mapping, view resolution, exception handling, and more. 翻译:DispatcherServlet 与任何 Servlet 一样,需要根据 Servlet 规范通过使用 Java 配置或在 web.xml 中进行声明和映射。 反过来,DispatcherServlet 使用 Spring 配置来发现请求映射、视图解析、异常处理等所需的委托组件。

官网提供了两种方式配置示例注册并初始化 DispatcherServlet的demo:

  1. JavaConfig方式
public class MyWebApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) {

        // Load Spring web application configuration
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(AppConfig.class);

        // Create and register the DispatcherServlet
        DispatcherServlet servlet = new DispatcherServlet(context);
        ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/app/*");
    }
}
  1. web.xml方式
<web-app>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/app-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>app</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>

</web-app>

推荐使用JavaConfig方式配置,无需xml文件,维护方便。

0 xml配置原理

从前面JavaConfig方式的demo中可以看出想要注册并初始化 DispatcherServlet,需要实现WebApplicationInitializer接口并重写onStartup(ServletContext context)方法。

WEB容器会在启动的时候去调onStartup方法。

这里说的WEB容器其实是一个Application Server,或者更准确的来说,是一个支持运行Servlet/JSP应用程序的容器。 比如:Tomcat 、JBoss、Jetty等。

通过传参ServletContext 用来注册web组件:

SpringMVC源码解析-0xml配置原理

那么onStart方法是如何生效的呢?是谁调用的?

我们实现的WebApplicationInitializer接口是springframework给提供的接口,那他是如何被web容器给调用的呢?

package org.springframework.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public interface WebApplicationInitializer {
    void onStartup(ServletContext servletContext) throws ServletException;
}

前面官方文档中提到DispatcherServlet 与任何 Servlet 一样,需要根据 Servlet 规范通过使用 Java 配置或在 web.xml 中进行声明和映射。什么是 Servlet 规范?

servlet规范是WEB框架在Servlet容器中运行时需要遵循的一种标准。 如果WEB框架想要在符合Servlet规范的容器中运行,那么它需要符合Servlet规范。 Servlet是Java对于Web开发而产生的一项技术,可以说Servlet技术是Java专有的。 学习链接: Java™ Servlet 规范,针对版本是 3.1。本文档描述了 Java Servlet API 的标准。

servlet 3.0版本以后提出的一个新规范SPI: 你的项目里面如果有某些类或者某些方法需要在启动的时候被Tomcat(Web容器)调用的话,首先你在你的项目的根目录的 META-INF/services/ javax.servlet.ServletContainerInitializer目录下指定一个文件。

SpringMVC源码解析-0xml配置原理

可以看出spring在遵从servlet规范的javax.servlet.ServletContainerInitializer中的指定了一个spring自己的类:org.springframework.web.SpringServletContainerInitializer。

@HandlesTypes({WebApplicationInitializer.class})
public class SpringServletContainerInitializer implements ServletContainerInitializer {
    public SpringServletContainerInitializer() {
    }

    public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException {
        //省略部分代码-----------
        while(var4.hasNext()) {
                WebApplicationInitializer initializer = (WebApplicationInitializer)var4.next();
                //关键代码
                initializer.onStartup(servletContext);
       }
    }
}

而SpringServletContainerInitializer类实现了ServletContainerInitializer接口,ServletContainerInitializer接口是javax.servlet包中提供的接口(即是servlet规范开发提供的接口),它也有一个onStartup方法,该方法会在容器启动的时候被调用。

public interface ServletContainerInitializer {
    void onStartup(Set<Class<?>> var1, ServletContext var2) throws ServletException;
}

那这个Servlet提供的ServletContainerInitializer接口的onStartup方法是怎么和Spring框架提供的WebApplicationInitializer接口的onStartup方法联系起来的呢?

刚才我们说的SpringServletContainerInitializer类实现了ServletContainerInitializer接口,该类上使用了注解@HandlesTypes({WebApplicationInitializer.class}),并重写了onStartup方法,该方法第一个参数是Set<Class<?>>;

@HandlesTypes注解由Servlet容器提供支持(实现),参数中指定的所有实现类,利用字节码扫描框架(例如ASM、BCEL)从classpath中扫描出来,放入集合,传给回调方法onStartup的第一个参数,然后执行其实现类的onStartup(servletContext)。

经此就显而易见了,我们的WebApplicationInitializer接口实现类就是在SpringServletContainerInitializer类中被循环调用的,在web容器启动后会调用ServletContainerInitializer实现类的onStartup方法,而该onStartup方法又会去调用我们的WebApplicationInitializer接口实现类中的onStartup方法,从而注册并初始化 DispatcherServlet以及其他web组件。

转载自:https://juejin.cn/post/7111914529528758286
评论
请登录