【Spring源码】SpringWeb容器_基于SpringBoot

简述

由于现在基本使用的都是 SpringBoot 项目来启动,所以我就直接建立在 SpringBoot 基础下看 WEB 容器了。

如果有一些项目开发经验就知道,SpringWEB 模块又名 SpringMVC,提供了视图数据整合以及可以直接返回 JSON 数据。前几年 JSP 当道的时候,各种 ViewResolver 层出不穷,据我所知的就有 FreeMarker JSP Beetl 等等。不过最近几年前端发生爆炸性变化,前后端能分离的都分离了,需要 SEO 的也很少使用 Java 来做了。所以现在 Java 后端项目的主要责职是提供业务所需要的交互数据给前端,不仅仅使用 JSON,还可以整合其他的传输协议,比如 Hessian XML Protobuf 等等,只要够折腾,什么格式都可以返回,不想折腾就使用 JSON 就可以了。

数据转换模块也会被 Spring 独立出来这个自然不需要再猜测,所以我们也可以为了减少传输使用的宽带,去定义第几个字节是什么东西。

那我们现在可以直接进入 SpringMVC 的源码来看看了。

初始化Web环境

JavaWEB 中,如果需要提供网络服务,一般是实现 Servlet 对象,像 Tomcat Resign 等网络容器都提供了自己的实现,我们就简单的用常用的 Tomcat 来说,Tomcat 会拿到我们项目中所有的 HTTPServlet 然后根据不同的 HTTP Method HTTP URL 来调用不同的 Servlet 方法来完成请求。

SpringMVC 是在 Servlet 级别的,所以这个容器必定会存在,那就是 DispatcherServlet。简单的理解 DispatcherServlet 就是一个在项目启动时读取了所有请求处理器,然后有请求进来根据不同的定义来调用不同的请求处理器的一个 Servlet 容器。

把断点打在 DispatcherServlet 的无参构造器上,找到初始化入口,是在 refreshContext(context) 中进行的,那我们就从这里开始:

public ConfigurableApplicationContext run(String... args) {
 StopWatch stopWatch = new StopWatch();
 stopWatch.start();
 ConfigurableApplicationContext context = null;
 Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
 configureHeadlessProperty();
 SpringApplicationRunListeners listeners = getRunListeners(args);
 listeners.starting();
 try {
  ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
  ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
  configureIgnoreBeanInfo(environment);
  Banner printedBanner = printBanner(environment);
  // 我们之前说过,这里会根据依赖的 jar 环境来创建不同的 Context
  // 所以现在创建的是 AnnotationConfigServletWebServerApplicationContext 容器
  context = createApplicationContext();
  exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
    new Class[] { ConfigurableApplicationContext.class }, context);
  prepareContext(context, environment, listeners, applicationArguments, printedBanner);
  // 刷新容器
  refreshContext(context);
  afterRefresh(context, applicationArguments);
  stopWatch.stop();
  if (this.logStartupInfo) {
   new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
  }
  listeners.started(context);
  callRunners(context, applicationArguments);
 }
 catch (Throwable ex) {
  handleRunFailure(context, ex, exceptionReporters, listeners);
  throw new IllegalStateException(ex);
 }

 try {
  listeners.running(context);
 }
 catch (Throwable ex) {
  handleRunFailure(context, ex, exceptionReporters, null);
  throw new IllegalStateException(ex);
 }
 return context;
}

一系列之前说过的步骤以后,来到这里:

protected void refresh(ApplicationContext applicationContext) {
 Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
 ((AbstractApplicationContext) applicationContext).refresh();
}

之前说过,调用 refresh 是个模板方法,AbstractApplicationContextrefresh 是个模板方法,会调用子类实现的 onRefresh。因为当前是个 WEB 项目,所以实现类 AnnotationConfigServletWebServerApplicationContextonRefresh 提供了实现:

@Override
protected void onRefresh() {
 super.onRefresh();
 try {
  createWebServer();
 }
 catch (Throwable ex) {
  throw new ApplicationContextException("Unable to start web server", ex);
 }
}

第一件事请 super.onRefresh() 是调用父级 GenericWebApplicationContext 的函数,不过这段函数现在看来应该没什么多大的用处了:

/**
 * 初始化界面主题容器
 */
@Override
protected void onRefresh() {
 this.themeSource = UiApplicationContextUtils.initThemeSource(this);
}

是一个初始化界面主题的函数,一开始我还怀疑这个主题是不是什么主题,然而进入 UiApplicationContextUtils.initThemeSource 看的时候,还真的就是 HTML 主题,里面定义了从哪里读取 CSS 呀这些鬼东西,跳过不看。

重点来看看 createWebServer() 做了哪些事情:

private void createWebServer() {
 WebServer webServer = this.webServer;
 ServletContext servletContext = getServletContext();
 if (webServer == null && servletContext == null) {
  // 工厂模式
  ServletWebServerFactory factory = getWebServerFactory();
  // 创建webServer
  this.webServer = factory.getWebServer(getSelfInitializer());
 }
 else if (servletContext != null) {
  try {
   getSelfInitializer().onStartup(servletContext);
  }
  catch (ServletException ex) {
   throw new ApplicationContextException("Cannot initialize servlet context", ex);
  }
 }
 // 初始化配置属性
 initPropertySources();
}

这时候获取了一个 ServletWebServerFactoryBeanDefinition,然后使用这个 Factory 来创建我们目前的 Web 环境。

protected ServletWebServerFactory getWebServerFactory() {
 // Use bean names so that we don't consider the hierarchy
 String[] beanNames = getBeanFactory().getBeanNamesForType(ServletWebServerFactory.class);
 if (beanNames.length == 0) {
  throw new ApplicationContextException("Unable to start ServletWebServerApplicationContext due to missing "
    + "ServletWebServerFactory bean.");
 }
 if (beanNames.length > 1) {
  throw new ApplicationContextException("Unable to start ServletWebServerApplicationContext due to multiple "
    + "ServletWebServerFactory beans : " + StringUtils.arrayToCommaDelimitedString(beanNames));
 }
 return getBeanFactory().getBean(beanNames[0], ServletWebServerFactory.class);
}

内置Tomcat注入

然而这个ServletWebServerFactory 什么时候偷偷被放进去的,我们现在快速过一遍:

  1. SpringBoot 启动的时候,在构造器调用了元信息读取器,读取了 META-INF/spring.factories 中配置的工厂,后初始化的时候会用到这些配置的类;
  2. SpringBoot 中上面的配置文件配置了这个工厂:ServletWebServerFactoryAutoConfiguration
  3. 看这个工厂有什么信息:
@Configuration(proxyBeanMethods = false)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnClass(ServletRequest.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(ServerProperties.class)
@Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,
        ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,
        ServletWebServerFactoryConfiguration.EmbeddedJetty.class,
        ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })
public class ServletWebServerFactoryAutoConfiguration {

 // 自定义容器的初始化:
 @Bean
 public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(ServerProperties serverProperties) {
  return new ServletWebServerFactoryCustomizer(serverProperties);
 }

 @Bean
 @ConditionalOnClass(name = "org.apache.catalina.startup.Tomcat")
 public TomcatServletWebServerFactoryCustomizer tomcatServletWebServerFactoryCustomizer(
   ServerProperties serverProperties) {
  return new TomcatServletWebServerFactoryCustomizer(serverProperties);
 }

 // 其他容器初始化...
}

然后我们看到上面导入那里导入了内置 Tomcat

@Configuration(proxyBeanMethods = false)
class ServletWebServerFactoryConfiguration {

 @Configuration(proxyBeanMethods = false)
 @ConditionalOnClass({ Servlet.class, Tomcat.class, UpgradeProtocol.class })
 @ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
 public static class EmbeddedTomcat {

  @Bean
  public TomcatServletWebServerFactory tomcatServletWebServerFactory(
    ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,
    ObjectProvider<TomcatContextCustomizer> contextCustomizers,
    ObjectProvider<TomcatProtocolHandlerCustomizer<?>> protocolHandlerCustomizers) {
   TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
   factory.getTomcatConnectorCustomizers()
     .addAll(connectorCustomizers.orderedStream().collect(Collectors.toList()));
   factory.getTomcatContextCustomizers()
     .addAll(contextCustomizers.orderedStream().collect(Collectors.toList()));
   factory.getTomcatProtocolHandlerCustomizers()
     .addAll(protocolHandlerCustomizers.orderedStream().collect(Collectors.toList()));
   return factory;
  }

 }
 //...
}

所以这个 ServletWebServerFactory 就在读取配置的时候被自动导入到 BeanDefinitionMaps 中去。

DispatcherServlet构建

怎么会涉及这一块的初始化呢,是因为上面 ServletWebServerFactoryConfiguration 初始化完成以后,触发的 ServletWebServerFactoryConfiguration 初始化,先来看看 ServletWebServerFactoryConfiguration 的注解:

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(DispatcherServlet.class)
@AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class)
public class DispatcherServletAutoConfiguration {

 /*
  * The bean name for a DispatcherServlet that will be mapped to the root URL "/"
  */
 public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet";

 /*
  * The bean name for a ServletRegistrationBean for the DispatcherServlet "/"
  */
 public static final String DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME = "dispatcherServletRegistration";

 @Configuration(proxyBeanMethods = false)
 @Conditional(DefaultDispatcherServletCondition.class)
 @ConditionalOnClass(ServletRegistration.class)
 @EnableConfigurationProperties({ HttpProperties.class, WebMvcProperties.class })
 protected static class DispatcherServletConfiguration {

  @Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
  public DispatcherServlet dispatcherServlet(HttpProperties httpProperties, WebMvcProperties webMvcProperties) {
   DispatcherServlet dispatcherServlet = new DispatcherServlet();
   dispatcherServlet.setDispatchOptionsRequest(webMvcProperties.isDispatchOptionsRequest());
   dispatcherServlet.setDispatchTraceRequest(webMvcProperties.isDispatchTraceRequest());
   dispatcherServlet.setThrowExceptionIfNoHandlerFound(webMvcProperties.isThrowExceptionIfNoHandlerFound());
   dispatcherServlet.setPublishEvents(webMvcProperties.isPublishRequestHandledEvents());
   dispatcherServlet.setEnableLoggingRequestDetails(httpProperties.isLogRequestDetails());
   return dispatcherServlet;
  }

  @Bean
  @ConditionalOnBean(MultipartResolver.class)
  @ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
  public MultipartResolver multipartResolver(MultipartResolver resolver) {
   // Detect if the user has created a MultipartResolver but named it incorrectly
   return resolver;
  }

 }
 // 其他暂时跳过的配置
}

这个初始化就很有灵性了,跟我们平常写的配置类大致一致。那关于 DispatcherServlet 的配置,我们均可以通过配置 WebMvcProperties 来让他注入到当前的 DispatcherServlet

这就完了😱?映射呢映射呢?

好吧,差点忘记此时 HTTPServlet 还没有初始化。

DispatcherServlet初始化(一)

默认的 HTTPServlet 在启动的时候只有装载,并没有初始化,初始化发生在第一次请求的时候。

我们来看看这个类:

public abstract class HttpServletBean extends HttpServlet implements EnvironmentCapable, EnvironmentAware {
  @Override
    public final void init() throws ServletException {

        // 读取配置.
        PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
        if (!pvs.isEmpty()) {
            try {
                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) {
                if (logger.isErrorEnabled()) {
                    logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
                }
                throw ex;
            }
        }

        // 子类实现初始化.
        initServletBean();
    }
}

FrameworkServlet 实现上面的方法

@Override
protected final void initServletBean() throws ServletException {
  getServletContext().log("Initializing Spring " + getClass().getSimpleName() + " '" + getServletName() + "'");
  if (logger.isInfoEnabled()) {
    logger.info("Initializing Servlet '" + getServletName() + "'");
  }
  long startTime = System.currentTimeMillis();

  try {
    // 初始化Web应用的上下文
    this.webApplicationContext = initWebApplicationContext();
    // 初始化
    initFrameworkServlet();
  }
  catch (ServletException | RuntimeException ex) {
    logger.error("Context initialization failed", ex);
    throw ex;
  }

  if (logger.isDebugEnabled()) {
    String value = this.enableLoggingRequestDetails ?
      "shown which may lead to unsafe logging of potentially sensitive data" :
    "masked to prevent unsafe logging of potentially sensitive data";
    logger.debug("enableLoggingRequestDetails='" + this.enableLoggingRequestDetails +
                 "': request parameters and headers will be " + value);
  }

  if (logger.isInfoEnabled()) {
    logger.info("Completed initialization in " + (System.currentTimeMillis() - startTime) + " ms");
  }
}
protected WebApplicationContext initWebApplicationContext() {
 WebApplicationContext rootContext =
   WebApplicationContextUtils.getWebApplicationContext(getServletContext());
 WebApplicationContext wac = null;

 if (this.webApplicationContext != null) {
  // 当前rootContext已经存在了
  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) {
  // No context instance is defined for this servlet -> create a local one
  wac = createWebApplicationContext(rootContext);
 }

 if (!this.refreshEventReceived) {
  // 刷新上下文
  synchronized (this.onRefreshMonitor) {
   onRefresh(wac);
  }
 }

 if (this.publishContext) {
  // Publish the context as a servlet context attribute.
  String attrName = getServletContextAttributeName();
  getServletContext().setAttribute(attrName, wac);
 }

 return wac;
}

刷新上下文在上面那么多个容器中已经很有感觉了,就是刷新配置的 Bean 调用对应的构造器 AOP 注入啊啊巴拉巴拉。

所以,onRefresh 是整个 Web 容器初始化的关键:

DispatcherServlet.java:
@Override
protected void onRefresh(ApplicationContext context) {
 initStrategies(context);
}
// 初始化DispatcherServlet所有的内容
protected void initStrategies(ApplicationContext context) {
  // 初始化多媒体处理器,处理上传的请求
  initMultipartResolver(context);
  // i18n初始化
  initLocaleResolver(context);
  // 项目主题
  initThemeResolver(context);
  // 请求处理器,如果我们写Controller应该是RequestMappingHandlerMapping处理
  initHandlerMappings(context);
  // 初始化适配器,会在调用的时候寻找合适的控制器和拦截器整合一起调用
  initHandlerAdapters(context);
  // 异常处理器
  initHandlerExceptionResolvers(context);
  // 视图名字处理器,通过控制器返回的字符串查找到对应的视图
  initRequestToViewNameTranslator(context);
  // 视图处理器,处理返回值与界面的整合
  initViewResolvers(context);
  // 初始化FlashMap处理器,FlashMap是一个保存数据的地方,
  // 在重定向的时候会把参数临时存储在session中
  // 跳转完成即清理上一个url携带的数据
  initFlashMapManager(context);
}

我大概只对 initHandlerMappingsinitHandlerAdapters 感兴趣。

private void initHandlerMappings(ApplicationContext context) {
 this.handlerMappings = null;

 if (this.detectAllHandlerMappings) {
  // 从BF中获取所有的HandlerMapping实例进行重组排序
  Map<String, HandlerMapping> matchingBeans =
    BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
  if (!matchingBeans.isEmpty()) {
   this.handlerMappings = new ArrayList<>(matchingBeans.values());
   // We keep HandlerMappings in sorted order.
   AnnotationAwareOrderComparator.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.
  }
 }

 if (this.handlerMappings == null) {
  this.handlerMappings = getDefaultStrategies(context, HandlerMapping.class);
  if (logger.isTraceEnabled()) {
   logger.trace("No HandlerMappings declared for servlet '" + getServletName() +
     "': using default strategies from DispatcherServlet.properties");
  }
 }
}

那么这些 handlerMappings 是什么时候被装配的,通过 WebMvcAutoConfiguration 这个配置类自动装配的,并且装配发生在 DispatcherServletAutoConfiguration 之后。

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
  ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

    // 重写父类createRequestMappingHandlerMapping,带有@Bean注解
    @Override
        protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
            if (this.mvcRegistrations != null && this.mvcRegistrations.getRequestMappingHandlerMapping() != null) {
                return this.mvcRegistrations.getRequestMappingHandlerMapping();
            }
            return super.createRequestMappingHandlerMapping();
        }

}

RequestMappingHandlerMapping装配

上面我们看到,RequestMappingHandler 是通过自动装配的形式放入 BeanFactory 的,那至于我们编写的 @Controller 以及 @RestController 是怎么被读取的,就是通过装配时,调用 InitializingBean 接口的 afterPropertiesSet 方法进行装配的。

// RequestMappingHandlerMapping.java
@Override
public void afterPropertiesSet() {
 this.config = new RequestMappingInfo.BuilderConfiguration();
 this.config.setUrlPathHelper(getUrlPathHelper());
 this.config.setPathMatcher(getPathMatcher());
 this.config.setSuffixPatternMatch(this.useSuffixPatternMatch);
 this.config.setTrailingSlashMatch(this.useTrailingSlashMatch);
 this.config.setRegisteredSuffixPatternMatch(this.useRegisteredSuffixPatternMatch);
 this.config.setContentNegotiationManager(getContentNegotiationManager());

 super.afterPropertiesSet();
}
// AbstractHandlerMethodMapping.java
@Override
public void afterPropertiesSet() {
  initHandlerMethods();
}
protected void initHandlerMethods() {
  for (String beanName : getCandidateBeanNames()) {
    if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
      processCandidateBean(beanName);
    }
  }
  handlerMethodsInitialized(getHandlerMethods());
}

拿到 BeanFactory 中所有的 Bean 一顿循环😶:

《【Spring源码】SpringWeb容器_基于SpringBoot》

// AbstractHandlerMethodMapping.java
protected void processCandidateBean(String beanName) {
 Class<?> beanType = null;
 try {
  beanType = obtainApplicationContext().getType(beanName);
 }
 catch (Throwable ex) {
  // An unresolvable bean type, probably from a lazy bean - let's ignore it.
  if (logger.isTraceEnabled()) {
   logger.trace("Could not resolve type for bean '" + beanName + "'", ex);
  }
 }
 // 关键是 isHandler,是处理器就注册
 if (beanType != null && isHandler(beanType)) {
  detectHandlerMethods(beanName);
 }
}

Bean@Controller 或者 @RequestMapping 就判定是一个控制器:

// isHandler: 
@Override
protected boolean isHandler(Class<?> beanType) {
 return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
   AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}

注册处理器:

// AbstractHandlerMethodMapping.java
protected void detectHandlerMethods(Object handler) {
 Class<?> handlerType = (handler instanceof String ?
   obtainApplicationContext().getType((String) handler) : handler.getClass());

 if (handlerType != null) {
  Class<?> userType = ClassUtils.getUserClass(handlerType);
  // 获取处理方法和对应映射的url
  // 处理映射url的时候还关系到配置的其他Path的混合等等
  Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
    (MethodIntrospector.MetadataLookup<T>) method -> {
     try {
      return getMappingForMethod(method, userType);
     }
     catch (Throwable ex) {
      throw new IllegalStateException("Invalid mapping on handler class [" +
        userType.getName() + "]: " + method, ex);
     }
    });
  if (logger.isTraceEnabled()) {
   logger.trace(formatMappings(userType, methods));
  }
  // 循环处理器和url,注册到当前容器
  methods.forEach((method, mapping) -> {
   // 获取代理方法,如果加上@Validate就会用这里,但是目前没有,先不看
   Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
   registerHandlerMethod(handler, invocableMethod, mapping);
  });
 }
}

接下来我们看注册控制器,Spring 又定义了一个 Mapping 容器,用于注册 :

// RequestMappingHandlerMapping.java:
@Override
protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {
  super.registerHandlerMethod(handler, method, mapping);
  // 然后根据情况,消费者的协议如果定义就更新条件
  updateConsumesCondition(mapping, method);
}
// AbstractHandlerMethodMapping.java
protected void registerHandlerMethod(Object handler, Method method, T mapping) {
 // 向下面定义的控制器容器注册处理器.
 this.mappingRegistry.register(mapping, handler, method);
}
/**
 * 一个持有所有控制器的容器
 */
class MappingRegistry {

  private final Map<T, MappingRegistration<T>> registry = new HashMap<>();

  private final Map<T, HandlerMethod> mappingLookup = new LinkedHashMap<>();

  // 通过url查找对应的处理器
  private final MultiValueMap<String, T> urlLookup = new LinkedMultiValueMap<>();

  private final Map<String, List<HandlerMethod>> nameLookup = new ConcurrentHashMap<>();

  private final Map<HandlerMethod, CorsConfiguration> corsLookup = new ConcurrentHashMap<>();

  private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

  //..
}

DispatcherServlet装配(二)

好了,在上面的装配中插入了 RequestMappingHandlerMapping 的装配,现在回来继续看两个方法:initHandlerMappingsinitHandlerAdapters

private void initHandlerMappings(ApplicationContext context) {
 this.handlerMappings = null;

 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()) {
   // 把 handlerMappings 缓存在 DispatcherServlet
   this.handlerMappings = new ArrayList<>(matchingBeans.values());
   // We keep HandlerMappings in sorted order.
   AnnotationAwareOrderComparator.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.
  }
 }

 // 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.isTraceEnabled()) {
   logger.trace("No HandlerMappings declared for servlet '" + getServletName() +
     "': using default strategies from DispatcherServlet.properties");
  }
 }
}

initHandlerAdapters 也差不多代码就不贴上来了。

处理HTTP请求准备

HTTPServlet 处理请求是在 service 方法中处理的,所以现在我们需要先进入核心的这个方法中去看:

FrameworkServlet 即这个 HTTPServlet 的第一个实现:

/**
 * Delegate GET requests to processRequest/doService.
 * <p>Will also be invoked by HttpServlet's default implementation of {@code doHead},
 * with a {@code NoBodyResponse} that just captures the content length.
 * @see #doService
 * @see #doHead
 */
@Override
protected final void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {

 processRequest(request, response);
}

/**
 * Delegate POST requests to {@link #processRequest}.
 * @see #doService
 */
@Override
protected final void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {

 processRequest(request, response);
}

/**
 * Delegate PUT requests to {@link #processRequest}.
 * @see #doService
 */
@Override
protected final void doPut(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {

 processRequest(request, response);
}

/**
 * Delegate DELETE requests to {@link #processRequest}.
 * @see #doService
 */
@Override
protected final void doDelete(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {

 processRequest(request, response);
}

那么其实在 FrameworkServlet 中,每一个 doXXX 都是交给 processRequest(request, response); 来做处理,所以现在我们主要看这个方法:

protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {

 long startTime = System.currentTimeMillis();
 Throwable failureCause = null;

 LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
 LocaleContext localeContext = buildLocaleContext(request);

 RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
 ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);

 // 当我们控制器方法是一个DeferredResult的时候会用到异步Web管理器,不过目前我们没有先跳过这部分
 WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
 asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());

 // RequestContextHolder初始化,将当前请求的参数设置到里面,这样我们就可以在任何地方拿到请求信息
 initContextHolders(request, localeContext, requestAttributes);

 try {
  // 在子类实现的部分
  doService(request, response);
 }
 catch (ServletException | IOException ex) {
  failureCause = ex;
  throw ex;
 }
 catch (Throwable ex) {
  failureCause = ex;
  throw new NestedServletException("Request processing failed", ex);
 }

 finally {
  // 清理RequestContextHolder
  resetContextHolders(request, previousLocaleContext, previousAttributes);
  if (requestAttributes != null) {
   requestAttributes.requestCompleted();
  }
  logResult(request, response, failureCause, asyncManager);
  // 发布请求事件,这里可以用来做操作记录吧,无论成功还是失败都能够拿到
  publishRequestHandledEvent(request, response, startTime, failureCause);
 }
}

查找控制器进行处理

protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
 logRequest(request);

 // 保存请求快照,下面恢复可以使用
 Map<String, Object> attributesSnapshot = null;
 if (WebUtils.isIncludeRequest(request)) {
  attributesSnapshot = new HashMap<>();
  Enumeration<?> attrNames = request.getAttributeNames();
  while (attrNames.hasMoreElements()) {
   String attrName = (String) attrNames.nextElement();
   if (this.cleanupAfterInclude || attrName.startsWith(DEFAULT_STRATEGIES_PREFIX)) {
    attributesSnapshot.put(attrName, request.getAttribute(attrName));
   }
  }
 }

 // 一些框架的设置,设置到Request中以供用户控制器使用
 request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());
 request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);
 request.setAttribute(THEME_RESOLVER_ATTRIBUTE, this.themeResolver);
 request.setAttribute(THEME_SOURCE_ATTRIBUTE, getThemeSource());

 // 保存flashMap
 if (this.flashMapManager != null) {
  FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(request, response);
  if (inputFlashMap != null) {
   request.setAttribute(INPUT_FLASH_MAP_ATTRIBUTE, Collections.unmodifiableMap(inputFlashMap));
  }
  request.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
  request.setAttribute(FLASH_MAP_MANAGER_ATTRIBUTE, this.flashMapManager);
 }

 try {
  // 开始查找执行的控制器
  doDispatch(request, response);
 }
 finally {
  if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
   // Restore the original attribute snapshot, in case of an include.
   if (attributesSnapshot != null) {
    restoreAttributesAfterInclude(request, attributesSnapshot);
   }
  }
 }
}

doDispatch 就是查找控制器,并且整合拦截器调用的过程:

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
 HttpServletRequest processedRequest = request;
 HandlerExecutionChain mappedHandler = null;
 boolean multipartRequestParsed = false;

 WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

 try {
  ModelAndView mv = null;
  Exception dispatchException = null;

  try {
   // 判断请求是否是上传的请求
   processedRequest = checkMultipart(request);
   multipartRequestParsed = (processedRequest != request);

   // 查找当前请求的处理器
   mappedHandler = getHandler(processedRequest);
   if (mappedHandler == null) {
    noHandlerFound(processedRequest, response);
    return;
   }

   // 查找适配器,用于整合一些拦截器等等
   HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

   // 处理 last-modified 请求头
   String method = request.getMethod();
   boolean isGet = "GET".equals(method);
   if (isGet || "HEAD".equals(method)) {
    long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
    if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
     return;
    }
   }

   if (!mappedHandler.applyPreHandle(processedRequest, response)) {
    return;
   }

   // 调用用户的控制器以及整合的拦截器处理请求,这时候请求会被写到Response中
   mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

   if (asyncManager.isConcurrentHandlingStarted()) {
    return;
   }

   // 处理视图,但是目前是JSON,是个空处理
   applyDefaultViewName(processedRequest, mv);
   // 执行其他的拦截器周期
   mappedHandler.applyPostHandle(processedRequest, response, mv);
  }
  catch (Exception ex) {
   dispatchException = ex;
  }
  catch (Throwable err) {
   // As of 4.3, we're processing Errors thrown from handler methods as well,
   // making them available for @ExceptionHandler methods and other scenarios.
   dispatchException = new NestedServletException("Handler dispatch failed", err);
  }
  processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
 }
 catch (Exception ex) {
  triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
 }
 catch (Throwable err) {
  triggerAfterCompletion(processedRequest, response, mappedHandler,
    new NestedServletException("Handler processing failed", err));
 }
 finally {
  if (asyncManager.isConcurrentHandlingStarted()) {
   // Instead of postHandle and afterCompletion
   if (mappedHandler != null) {
    mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
   }
  }
  else {
   // Clean up any resources used by a multipart request.
   if (multipartRequestParsed) {
    cleanupMultipart(processedRequest);
   }
  }
 }
}

目前的拦截器链有四个:

  1. RequestMappingHandlerAdapter
  2. HandlerFunctionAdapter
  3. HttpRequestHandlerAdapter
  4. SimpleControllerHandlerAdapter

在查找的过程中,返回第一个匹配的适配器:

protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
 if (this.handlerAdapters != null) {
  for (HandlerAdapter adapter : this.handlerAdapters) {
   if (adapter.supports(handler)) {
    return adapter;
   }
  }
 }
 throw new ServletException("No adapter for handler [" + handler +
   "]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
}

那么我们现在拿到的实例就是 RequestMappingHandlerAdapter

然后看看调用 mv = ha.handle(processedRequest, response, mappedHandler.getHandler()) 做什么什么事情:

@Override
@Nullable
public final ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
  throws Exception {

 return handleInternal(request, response, (HandlerMethod) handler);
}
@Override
protected ModelAndView handleInternal(HttpServletRequest request,
  HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {

 ModelAndView mav;
 checkRequest(request);

 // 并发操作,查询是否需要加锁处理
 if (this.synchronizeOnSession) {
  HttpSession session = request.getSession(false);
  if (session != null) {
   Object mutex = WebUtils.getSessionMutex(session);
   synchronized (mutex) {
    mav = invokeHandlerMethod(request, response, handlerMethod);
   }
  }
  else {
   // No HttpSession available -> no mutex necessary
   mav = invokeHandlerMethod(request, response, handlerMethod);
  }
 }
 else {
  // 没有Session需要同步,直接走这里
  mav = invokeHandlerMethod(request, response, handlerMethod);
 }

 if (!response.containsHeader(HEADER_CACHE_CONTROL)) {
  if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
   applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
  }
  else {
   prepareResponse(response);
  }
 }

 return mav;
}

构建请求处理:

protected ModelAndView invokeHandlerMethod(HttpServletRequest request,
  HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {

 // 将request和response封装成ServletWebRequest对象
 ServletWebRequest webRequest = new ServletWebRequest(request, response);
 try {
  // 获取WebDataBinderFactory工厂处理表单多选项的数据绑定问题
  // 如CheckBox需要绑定到对象的集合中
  WebDataBinderFactory binderFactory = getDataBinderFactory(handlerMethod);
  // 处理参数为Model方法的工厂
  ModelFactory modelFactory = getModelFactory(handlerMethod, binderFactory);

  // 设置参数处理器,包括{PATH}、RequestParam等的处理,以及返回值转换处理器
  ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(handlerMethod);
  if (this.argumentResolvers != null) {
   invocableMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
  }
  if (this.returnValueHandlers != null) {
   invocableMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
  }
  invocableMethod.setDataBinderFactory(binderFactory);
  invocableMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);

  // 初始化执行容器 ModelAndViewContainer
  ModelAndViewContainer mavContainer = new ModelAndViewContainer();
  mavContainer.addAllAttributes(RequestContextUtils.getInputFlashMap(request));
  modelFactory.initModel(webRequest, mavContainer, invocableMethod);
  mavContainer.setIgnoreDefaultModelOnRedirect(this.ignoreDefaultModelOnRedirect);

  AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request, response);
  asyncWebRequest.setTimeout(this.asyncRequestTimeout);

  WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
  asyncManager.setTaskExecutor(this.taskExecutor);
  asyncManager.setAsyncWebRequest(asyncWebRequest);
  asyncManager.registerCallableInterceptors(this.callableInterceptors);
  asyncManager.registerDeferredResultInterceptors(this.deferredResultInterceptors);

  // 异步处理器的处理
  if (asyncManager.hasConcurrentResult()) {
   Object result = asyncManager.getConcurrentResult();
   mavContainer = (ModelAndViewContainer) asyncManager.getConcurrentResultContext()[0];
   asyncManager.clearConcurrentResult();
   LogFormatUtils.traceDebug(logger, traceOn -> {
    String formatted = LogFormatUtils.formatValue(result, !traceOn);
    return "Resume with async result [" + formatted + "]";
   });
   invocableMethod = invocableMethod.wrapConcurrentResult(result);
  }

  // 执行调用处理器
  invocableMethod.invokeAndHandle(webRequest, mavContainer);
  if (asyncManager.isConcurrentHandlingStarted()) {
   return null;
  }

  return getModelAndView(mavContainer, modelFactory, webRequest);
 }
 finally {
  webRequest.requestCompleted();
 }
}

《【Spring源码】SpringWeb容器_基于SpringBoot》

《【Spring源码】SpringWeb容器_基于SpringBoot》

ServletInvocableHandlerMethod处理请求调用

拿到调用的 ServletWebRequest 以及 ModelAndViewContainer 开始处理请求

public void invokeAndHandle(ServletWebRequest webRequest, ModelAndViewContainer mavContainer,
  Object... providedArgs) throws Exception {

 // 转换请求参数到目标方法,调用方法并拿到返回值处理
 Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs);
 // 处理相应状态
 setResponseStatus(webRequest);

 if (returnValue == null) {
  if (isRequestNotModified(webRequest) || getResponseStatus() != null || mavContainer.isRequestHandled()) {
   disableContentCachingIfNecessary(webRequest);
   mavContainer.setRequestHandled(true);
   return;
  }
 }
 else if (StringUtils.hasText(getResponseStatusReason())) {
  mavContainer.setRequestHandled(true);
  return;
 }

 mavContainer.setRequestHandled(false);
 Assert.state(this.returnValueHandlers != null, "No return value handlers");
 try {
  // 返回值处理器将控制器的返回值处理成客户端期待的协议,比如JSON
  this.returnValueHandlers.handleReturnValue(
    returnValue, getReturnValueType(returnValue), mavContainer, webRequest);
 }
 catch (Exception ex) {
  if (logger.isTraceEnabled()) {
   logger.trace(formatErrorForReturnValue(returnValue), ex);
  }
  throw ex;
 }
}

转换请求:

@Nullable
public Object invokeForRequest(NativeWebRequest request, @Nullable ModelAndViewContainer mavContainer,
  Object... providedArgs) throws Exception {

 Object[] args = getMethodArgumentValues(request, mavContainer, providedArgs);
 if (logger.isTraceEnabled()) {
  logger.trace("Arguments: " + Arrays.toString(args));
 }
 return doInvoke(args);
}
// 获取方法的参数签名
protected Object[] getMethodArgumentValues(NativeWebRequest request, @Nullable ModelAndViewContainer mavContainer,
                                           Object... providedArgs) throws Exception {

  // 当前没有请求参数,直接返回空的参数值
  MethodParameter[] parameters = getMethodParameters();
  if (ObjectUtils.isEmpty(parameters)) {
    return EMPTY_ARGS;
  }

  Object[] args = new Object[parameters.length];
  for (int i = 0; i < parameters.length; i++) {
    MethodParameter parameter = parameters[i];
    parameter.initParameterNameDiscovery(this.parameterNameDiscoverer);
    args[i] = findProvidedArgument(parameter, providedArgs);
    if (args[i] != null) {
      continue;
    }
    if (!this.resolvers.supportsParameter(parameter)) {
      throw new IllegalStateException(formatArgumentError(parameter, "No suitable resolver"));
    }
    try {
      // 调用处理器翻译请求参数到JavaBean
      // 里层就是轮询所有翻译器,返回能够执行的进行调用
      args[i] = this.resolvers.resolveArgument(parameter, mavContainer, request, this.dataBinderFactory);
    }
    catch (Exception ex) {
      // Leave stack trace for later, exception may actually be resolved and handled...
      if (logger.isDebugEnabled()) {
        String exMsg = ex.getMessage();
        if (exMsg != null && !exMsg.contains(parameter.getExecutable().toGenericString())) {
          logger.debug(formatArgumentError(parameter, exMsg));
        }
      }
      throw ex;
    }
  }
  return args;
}

通过反射调用控制器方法执行:

@Nullable
protected Object doInvoke(Object... args) throws Exception {
 ReflectionUtils.makeAccessible(getBridgedMethod());
 try {
  // 传递当前对象以及参数执行,拿到返回值进行返回
  return getBridgedMethod().invoke(getBean(), args);
 }
 catch (IllegalArgumentException ex) {
  assertTargetBean(getBridgedMethod(), getBean(), args);
  String text = (ex.getMessage() != null ? ex.getMessage() : "Illegal argument");
  throw new IllegalStateException(formatInvokeError(text, args), ex);
 }
 catch (InvocationTargetException ex) {
  // Unwrap for HandlerExceptionResolvers ...
  Throwable targetException = ex.getTargetException();
  if (targetException instanceof RuntimeException) {
   throw (RuntimeException) targetException;
  }
  else if (targetException instanceof Error) {
   throw (Error) targetException;
  }
  else if (targetException instanceof Exception) {
   throw (Exception) targetException;
  }
  else {
   throw new IllegalStateException(formatInvokeError("Invocation failure", args), targetException);
  }
 }
}

HandlerMethodReturnValueHandlerComposite处理返回值写出

循环所有消息处理器,拿到第一个支持的 HandlerMethodReturnValueHandler 调用处理器将结果写出。

@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
  ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

 // 拿到RequestResponseBodyMethodProcessor处理器进行结果处理
 HandlerMethodReturnValueHandler handler = selectHandler(returnValue, returnType);
 if (handler == null) {
  throw new IllegalArgumentException("Unknown return value type: " + returnType.getParameterType().getName());
 }
 handler.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
}

@Nullable
private HandlerMethodReturnValueHandler selectHandler(@Nullable Object value, MethodParameter returnType) {
 boolean isAsyncValue = isAsyncReturnValue(value, returnType);
 for (HandlerMethodReturnValueHandler handler : this.returnValueHandlers) {
  if (isAsyncValue && !(handler instanceof AsyncHandlerMethodReturnValueHandler)) {
   continue;
  }
  if (handler.supportsReturnType(returnType)) {
   return handler;
  }
 }
 return null;
}
// AbstractMessageConverterMethodProcessor.java
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
  ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
  throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {

 mavContainer.setRequestHandled(true);
 // 重新封装Request和Response
 ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
 ServletServerHttpResponse outputMessage = createOutputMessage(webRequest);

 // 父级方法,执行消息转换
 writeWithMessageConverters(returnValue, returnType, inputMessage, outputMessage);
}

获取返回值,根据 HTTPHeaders 进行匹配转换器,转换写入 Response 流中。

protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter returnType,
  ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage)
  throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {

 Object body;
 Class<?> valueType;
 Type targetType;

 // 判断返回值
 if (value instanceof CharSequence) {
  body = value.toString();
  valueType = String.class;
  targetType = String.class;
 }
 else {
  body = value;
  valueType = getReturnValueType(body, returnType);
  targetType = GenericTypeResolver.resolveType(getGenericType(returnType), returnType.getContainingClass());
 }

 // 是不是写出流,用于文件下载
 if (isResourceType(value, returnType)) {
  outputMessage.getHeaders().set(HttpHeaders.ACCEPT_RANGES, "bytes");
  if (value != null && inputMessage.getHeaders().getFirst(HttpHeaders.RANGE) != null &&
    outputMessage.getServletResponse().getStatus() == 200) {
   Resource resource = (Resource) value;
   try {
    List<HttpRange> httpRanges = inputMessage.getHeaders().getRange();
    outputMessage.getServletResponse().setStatus(HttpStatus.PARTIAL_CONTENT.value());
    body = HttpRange.toResourceRegions(httpRanges, resource);
    valueType = body.getClass();
    targetType = RESOURCE_REGION_LIST_TYPE;
   }
   catch (IllegalArgumentException ex) {
    outputMessage.getHeaders().set(HttpHeaders.CONTENT_RANGE, "bytes */" + resource.contentLength());
    outputMessage.getServletResponse().setStatus(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE.value());
   }
  }
 }

 // 根据Content-Type或Accept进行匹配转换器
 MediaType selectedMediaType = null;
 MediaType contentType = outputMessage.getHeaders().getContentType();
 boolean isContentTypePreset = contentType != null && contentType.isConcrete();
 if (isContentTypePreset) {
  if (logger.isDebugEnabled()) {
   logger.debug("Found 'Content-Type:" + contentType + "' in response");
  }
  selectedMediaType = contentType;
 }
 else {
  HttpServletRequest request = inputMessage.getServletRequest();
  // 客户端请求的MediaType
  List<MediaType> acceptableTypes = getAcceptableMediaTypes(request);
  // 项目允许的MediaType
  List<MediaType> producibleTypes = getProducibleMediaTypes(request, valueType, targetType);

  if (body != null && producibleTypes.isEmpty()) {
   throw new HttpMessageNotWritableException(
     "No converter found for return value of type: " + valueType);
  }
  // 轮询获取可以使用的MediaType
  List<MediaType> mediaTypesToUse = new ArrayList<>();
  for (MediaType requestedType : acceptableTypes) {
   for (MediaType producibleType : producibleTypes) {
    if (requestedType.isCompatibleWith(producibleType)) {
     mediaTypesToUse.add(getMostSpecificMediaType(requestedType, producibleType));
    }
   }
  }
  if (mediaTypesToUse.isEmpty()) {
   if (body != null) {
    throw new HttpMediaTypeNotAcceptableException(producibleTypes);
   }
   if (logger.isDebugEnabled()) {
    logger.debug("No match for " + acceptableTypes + ", supported: " + producibleTypes);
   }
   return;
  }

  MediaType.sortBySpecificityAndQuality(mediaTypesToUse);

  // 遍历拿到第一个可以执行的MediaType
  for (MediaType mediaType : mediaTypesToUse) {
   if (mediaType.isConcrete()) {
    selectedMediaType = mediaType;
    break;
   }
   else if (mediaType.isPresentIn(ALL_APPLICATION_MEDIA_TYPES)) {
    selectedMediaType = MediaType.APPLICATION_OCTET_STREAM;
    break;
   }
  }

  if (logger.isDebugEnabled()) {
   logger.debug("Using '" + selectedMediaType + "', given " +
     acceptableTypes + " and supported " + producibleTypes);
  }
 }

 if (selectedMediaType != null) {
  selectedMediaType = selectedMediaType.removeQualityValue();
  // 轮询HttpMessageConverter拿到可以应用于当前MediaType和返回值的转换器
  for (HttpMessageConverter<?> converter : this.messageConverters) {
   GenericHttpMessageConverter genericConverter = (converter instanceof GenericHttpMessageConverter ?
     (GenericHttpMessageConverter<?>) converter : null);
   // 判断是否可写,此时拿到MappingJackson2HttpMessageConverter
   if (genericConverter != null ?
     ((GenericHttpMessageConverter) converter).canWrite(targetType, valueType, selectedMediaType) :
     converter.canWrite(valueType, selectedMediaType)) {
    body = getAdvice().beforeBodyWrite(body, returnType, selectedMediaType,
      (Class<? extends HttpMessageConverter<?>>) converter.getClass(),
      inputMessage, outputMessage);
    if (body != null) {
     Object theBody = body;
     LogFormatUtils.traceDebug(logger, traceOn ->
       "Writing [" + LogFormatUtils.formatValue(theBody, !traceOn) + "]");
     // 如果是下载文件新增必要的HTTP头信息
     addContentDispositionHeader(inputMessage, outputMessage);
     if (genericConverter != null) {
      // 写出
      genericConverter.write(body, targetType, selectedMediaType, outputMessage);
     }
     else {
      ((HttpMessageConverter) converter).write(body, selectedMediaType, outputMessage);
     }
    }
    else {
     if (logger.isDebugEnabled()) {
      logger.debug("Nothing to write: null body");
     }
    }
    return;
   }
  }
 }

 if (body != null) {
  if (isContentTypePreset) {
   throw new HttpMessageNotWritableException(
     "No converter for [" + valueType + "] with preset Content-Type '" + contentType + "'");
  }
  throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes);
 }
}
@Override
public final void write(final T t, @Nullable final Type type, @Nullable MediaType contentType,
  HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {

 final HttpHeaders headers = outputMessage.getHeaders();
 addDefaultHeaders(headers, t, contentType);

 // 判断是否支持流信息写出
 if (outputMessage instanceof StreamingHttpOutputMessage) {
  StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
  streamingOutputMessage.setBody(outputStream -> writeInternal(t, type, new HttpOutputMessage() {
   @Override
   public OutputStream getBody() {
    return outputStream;
   }
   @Override
   public HttpHeaders getHeaders() {
    return headers;
   }
  }));
 }
 else {
  // 写出并刷新  
  writeInternal(t, type, outputMessage);
  outputMessage.getBody().flush();
 }
}

使用 JSON 流的形式写出:

@Override
protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage)
  throws IOException, HttpMessageNotWritableException {

 MediaType contentType = outputMessage.getHeaders().getContentType();
 JsonEncoding encoding = getJsonEncoding(contentType);
 JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
 try {
  writePrefix(generator, object);

  Object value = object;
  Class<?> serializationView = null;
  FilterProvider filters = null;
  JavaType javaType = null;

  if (object instanceof MappingJacksonValue) {
   MappingJacksonValue container = (MappingJacksonValue) object;
   value = container.getValue();
   serializationView = container.getSerializationView();
   filters = container.getFilters();
  }
  if (type != null && TypeUtils.isAssignable(type, value.getClass())) {
   javaType = getJavaType(type, null);
  }

  ObjectWriter objectWriter = (serializationView != null ?
    this.objectMapper.writerWithView(serializationView) : this.objectMapper.writer());
  if (filters != null) {
   objectWriter = objectWriter.with(filters);
  }
  if (javaType != null && javaType.isContainerType()) {
   objectWriter = objectWriter.forType(javaType);
  }
  SerializationConfig config = objectWriter.getConfig();
  if (contentType != null && contentType.isCompatibleWith(MediaType.TEXT_EVENT_STREAM) &&
    config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
   objectWriter = objectWriter.with(this.ssePrettyPrinter);
  }
  // 写入JSON值
  objectWriter.writeValue(generator, value);

  writeSuffix(generator, object);
  generator.flush();
 }
 catch (InvalidDefinitionException ex) {
  throw new HttpMessageConversionException("Type definition error: " + ex.getType(), ex);
 }
 catch (JsonProcessingException ex) {
  throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getOriginalMessage(), ex);
 }
}

doDispatch直接其他善后操作

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
  // ...
  // Actually invoke the handler.
  mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

  if (asyncManager.isConcurrentHandlingStarted()) {
   return;
  }

  applyDefaultViewName(processedRequest, mv);
  mappedHandler.applyPostHandle(processedRequest, response, mv);
  // ...
}

后面的工作就是轮询拦截器执行后处理的操作。

/**
 * Apply postHandle methods of registered interceptors.
 */
void applyPostHandle(HttpServletRequest request, HttpServletResponse response, @Nullable ModelAndView mv)
  throws Exception {

 HandlerInterceptor[] interceptors = getInterceptors();
 if (!ObjectUtils.isEmpty(interceptors)) {
  for (int i = interceptors.length - 1; i >= 0; i--) {
   HandlerInterceptor interceptor = interceptors[i];
   // 调用我们写的拦截器
   interceptor.postHandle(request, response, this.handler, mv);
  }
 }
}

点赞