HttpRequestHandlerAdapter——SpringMVC中的网络请求处理适配器

前置知识:我们SpringMVC中,DispatcherServlet拿到请求,先去HandlerMapping找到handler链,然后获取支持这个handler的HandlerAdapter,拿得到的话就执行处理逻辑,然后得到模型视图,再解析视图,渲染。

有了这些知识,我们再来看看这个类
HttpRequestHandlerAdapter:

package org.springframework.web.servlet.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.lang.Nullable;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;

/**
 * Adapter to use the plain {@link org.springframework.web.HttpRequestHandler}
 * interface with the generic {@link org.springframework.web.servlet.DispatcherServlet}.
 * Supports handlers that implement the {@link LastModified} interface.
 *
 * <p>This is an SPI class, not used directly by application code.
 *
 * @author Juergen Hoeller
 * @since 2.0
 * @see org.springframework.web.servlet.DispatcherServlet
 * @see org.springframework.web.HttpRequestHandler
 * @see SimpleControllerHandlerAdapter
 */
public class HttpRequestHandlerAdapter implements HandlerAdapter {

	@Override
	public boolean supports(Object handler) {
		return (handler instanceof HttpRequestHandler);
	}

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

		((HttpRequestHandler) handler).handleRequest(request, response);
		return null;
	}

	@Override
	@SuppressWarnings("deprecation")
	public long getLastModified(HttpServletRequest request, Object handler) {
		if (handler instanceof LastModified) {
			return ((LastModified) handler).getLastModified(request);
		}
		return -1L;
	}
}

它提示这是一个SPI类,应用程序代码不会直接使用它。

什么是SPI:
SPI(Service Provider Interface),Java在语言层面为我们提供了一种方便地创建可扩展应用的途径。SPI提供了一种JVM级别的服务发现机制,我们只需要按照SPI的要求,在jar包中进行适当的配置,jvm就会在运行时通过懒加载,帮我们找到所需的服务并加载。如果我们一直不使用某个服务,那么它不会被加载,一定程度上避免了资源的浪费。
Java 提供了很多服务提供者接口(Service Provider Interface,SPI),允许第三方为这些接口提供实现。常见的 SPI 有 JDBC、JNDI、JAXP 等,这些SPI的接口由核心类库提供,却由第三方实现。
详情可以看这篇文章:https://blog.csdn.net/john1337/article/details/92803089

也就是说,这个适配器,其实是实现拓展用的。
就像提到的HttpRequestHandler,其实是一个接口而已,我们supports查看是否是HttpRequestHandler(的实现),是就是这个适配器可以适用于那个handler,然后调用handleRequest(子类自己实现),让handler去解决。

HttpRequestHandler子类有这么一些:
HttpRequestHandlerAdapter——SpringMVC中的网络请求处理适配器
这里引出两个比较重要的类:
org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
org.springframework.web.servlet.resource.ResourceHttpRequestHandler

先看看DefaultServletHttpRequestHandler:


/**
 * An {@link HttpRequestHandler} for serving static files using the Servlet container's "default" Servlet.
 *
 * <p>This handler is intended to be used with a "/*" mapping when the
 * {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet}
 * is mapped to "/", thus  overriding the Servlet container's default handling of static resources.
 * The mapping to this handler should generally be ordered as the last in the chain so that it will
 * only execute when no other more specific mappings (i.e., to controllers) can be matched.
 *
 * <p>Requests are handled by forwarding through the {@link RequestDispatcher} obtained via the
 * name specified through the {@link #setDefaultServletName "defaultServletName" property}.
 * In most cases, the {@code defaultServletName} does not need to be set explicitly, as the
 * handler checks at initialization time for the presence of the default Servlet of well-known
 * containers such as Tomcat, Jetty, Resin, WebLogic and WebSphere. However, when running in a
 * container where the default Servlet's name is not known, or where it has been customized
 * via server configuration, the  {@code defaultServletName} will need to be set explicitly.
 *
 * @author Jeremy Grelle
 * @author Juergen Hoeller
 * @since 3.0.4
 */
 public class DefaultServletHttpRequestHandler implements HttpRequestHandler, ServletContextAware

翻译:

使用Servlet容器的“默认”Servlet来提供静态文件的{@link HttpRequestHandler}。

此处理程序用于“/*”映射时
{@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet}被映射到"/",从而覆盖Servlet容器对静态资源的默认处理。
到这个处理器的映射通常应该被排序为链中的最后一个只有当没有其他更具体的映射(例如,到控制器)可以匹配时才执行。

请求是通过转发{@link RequestDispatcher}来处理的
通过{@link #setDefaultServletName "defaultServletName"属性}指定的名称。
在大多数情况下,{@code defaultServletName}不需要显式设置,作为
处理程序在初始化时检查是否存在已知的默认Servlet
容器,如Tomcat, Jetty, Resin, WebLogic和WebSphere。然而,当运行在一个
容器,其中默认Servlet的名称是未知的,或者在哪里定制了它
通过服务器配置,{@code defaultServletName}需要显式设置。

再看看ResourceHttpRequestHandler:

/**
 * {@code HttpRequestHandler} that serves static resources in an optimized way
 * according to the guidelines of Page Speed, YSlow, etc.
 *
 * <p>The properties {@linkplain #setLocations "locations"} and
 * {@linkplain #setLocationValues "locationValues"} accept locations from which
 * static resources can be served by this handler. This can be relative to the
 * root of the web application, or from the classpath, e.g.
 * "classpath:/META-INF/public-web-resources/", allowing convenient packaging
 * and serving of resources such as .js, .css, and others in jar files.
 *
 * <p>This request handler may also be configured with a
 * {@link #setResourceResolvers(List) resourcesResolver} and
 * {@link #setResourceTransformers(List) resourceTransformer} chains to support
 * arbitrary resolution and transformation of resources being served. By default
 * a {@link PathResourceResolver} simply finds resources based on the configured
 * "locations". An application can configure additional resolvers and transformers
 * such as the {@link VersionResourceResolver} which can resolve and prepare URLs
 * for resources with a version in the URL.
 *
 * <p>This handler also properly evaluates the {@code Last-Modified} header
 * (if present) so that a {@code 304} status code will be returned as appropriate,
 * avoiding unnecessary overhead for resources that are already cached by the client.
 *
 * @author Keith Donald
 * @author Jeremy Grelle
 * @author Juergen Hoeller
 * @author Arjen Poutsma
 * @author Brian Clozel
 * @author Rossen Stoyanchev
 * @since 3.0.4
 */
 public class ResourceHttpRequestHandler extends WebContentGenerator
		implements HttpRequestHandler, EmbeddedValueResolverAware, InitializingBean, CorsConfigurationSource

翻译:

{@code HttpRequestHandler}根据Page Speed, YSlow等的指导方针,以优化的方式提供静态资源。

属性{@linkplain #setLocations “locations”}和
{@linkplain #setLocationValues “locationValues”}接受locations,这些location的静态资源可以由这个处理程序提供。这可以是相对于web应用程序的根目录,或者从类路径开始,等等。
“类路径:/META-INF/public-web-resources/”,方便打包,并在jar文件中提供.js、.css等资源。

此请求处理程序还可以配置为
{@link #setResourceResolvers(List) resourcesResolver}和
{@link # setresourcetransformer (List) resourceTransformer}支持的链
任意解决和转换所服务的资源。默认情况下
一个{@link PathResourceResolver}简单地根据配置查找资源“位置”。应用程序可以配置额外的resolvers和transformers
例如{@link VersionResourceResolver},它可以为URL中有版本的资源解析和准备URLs。

这个处理程序也正确地计算{@code Last-Modified}头文件(如果存在)以便返回{@code 304}状态码,避免客户端已经缓存的资源的不必要开销。

总结:
HttpRequestHandlerAdapter就是为了适配Http请求处理器handler用的
在请求过来的时候,我们有一大堆可能的处理器handler(根据网络映射路径)排队检验,但是我们从众多的适配器中选择第一个支持的适配器(大多数时候就是HttpRequestHandlerAdapter,因为它支持HttpRequestHandler),然后它调用相应的HttpRequestHandler(的实例)对请求进行处理。

上一篇:超详细讲解SpringMVC三层架构


下一篇:基于Java+springmvc+mysql+jquery实现企业员工管理系统