springmvc揭秘之HandlerAdapter处理适配器

HandlerAdapter处理适配器

HandlerMapping通过request找到了handler,HandlerAdapter是具体使用Handler来干活的,每个HandlerAdapter封装了一种Handler的具体使用方法

由于Spring经过很长时间的版本迭代,为了适配老版本,Spring 中的处理器的实现有很多种方式,比如可以实现 Controller 接口,也可以用 @Controller+@RequestMapping 注解将方法作为一个处理器等,这就导致 Spring 不知道怎么调用用户的处理器逻辑。因此需要一个处理器适配器,由处理器适配器去调用处理器的逻辑

HandlerAdapter按照特定规则去执行Handler,通过扩展适配器可以对更多类型的处理器进行执行

java 复制代码
public interface HandlerAdapter {

   // 判断是否可以使用某个Handler
   boolean supports(Object handler);

   // 具体使用handler完成工作
   ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;

   // 获取Last-Modified资源最后一次修改时间
   long getLastModified(HttpServletRequest request, Object handler);

}
xml 复制代码
<bean id="annotationMethodHandlerAdapter"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
  • HttpRequestHandlerAdapter 用于适配HttpRequestHandler,处理实现了HttpRequestHandler接口的handler

    java 复制代码
    public class HttpRequestHandlerAdapter implements HandlerAdapter {
    
       @Override
       public boolean supports(Object handler) {
          return (handler instanceof HttpRequestHandler);
       }
    
       @Override
       public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
             throws Exception {
    
          ((HttpRequestHandler) handler).handleRequest(request, response);
          return null;
       }
    
       @Override
       public long getLastModified(HttpServletRequest request, Object handler) {
          if (handler instanceof LastModified) {
             return ((LastModified) handler).getLastModified(request);
          }
          return -1L;
       }
    
    }
  • SimpleControllerHandlerAdapter 用于适配Controller,处理实现了Controller接口的handler

    java 复制代码
    public class SimpleControllerHandlerAdapter implements HandlerAdapter {
    
       @Override
       public boolean supports(Object handler) {
          return (handler instanceof Controller);
       }
    
       @Override
       public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
             throws Exception {
    
          return ((Controller) handler).handleRequest(request, response);
       }
    
       @Override
       public long getLastModified(HttpServletRequest request, Object handler) {
          if (handler instanceof LastModified) {
             return ((LastModified) handler).getLastModified(request);
          }
          return -1L;
       }
    
    }
  • SimpleServletHandlerAdapter 用于适配Servlet,处理实现了Servlet接口的handler

    java 复制代码
    public class SimpleServletHandlerAdapter implements HandlerAdapter {
    
       @Override
       public boolean supports(Object handler) {
          return (handler instanceof Servlet);
       }
    
       @Override
       public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
             throws Exception {
    
          ((Servlet) handler).service(request, response);
          return null;
       }
    
       @Override
       public long getLastModified(HttpServletRequest request, Object handler) {
          return -1;
       }
    
    }
  • RequestMappingHandlerAdapter 用于适配HandlerMethod,这个就比较复杂了,下边单独拿出来说明

RequestMappingHandlerAdapter

平常使用最多的也是RequestMappingHandlerAdapter,可以看到它是处理HandlerMethod类的

java 复制代码
public final boolean supports(Object handler) {
   return (handler instanceof HandlerMethod && supportsInternal((HandlerMethod) handler));
}

就是使用@Controller和@RequestMapping来进行映射的方法

该类实现了InitializingBean接口,会执行afterPropertiesSet方法

java 复制代码
public void afterPropertiesSet() {
   // Do this first, it may add ResponseBody advice beans
  // @ControllerAdvice的bean
   initControllerAdviceCache();

   if (this.argumentResolvers == null) {
     // 一些参数解析器,如解析@RequestParam、@PathVariable、@RequestBody等
      List<HandlerMethodArgumentResolver> resolvers = getDefaultArgumentResolvers();
      this.argumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
   }
   if (this.initBinderArgumentResolvers == null) {
     // initBinder的参数解析器
      List<HandlerMethodArgumentResolver> resolvers = getDefaultInitBinderArgumentResolvers();
      this.initBinderArgumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
   }
   if (this.returnValueHandlers == null) {
     // 返回值的参数解析器,如ModelAndView、ResponseEntity、HttpEntity、@ResponseBody注解等
      List<HandlerMethodReturnValueHandler> handlers = getDefaultReturnValueHandlers();
      this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite().addHandlers(handlers);
   }
}

处理请求

java 复制代码
protected ModelAndView handleInternal(HttpServletRequest request,
      HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {

   ModelAndView mav;
   checkRequest(request);

   // Execute invokeHandlerMethod in synchronized block if required.
   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 {
      // No synchronization on session demanded at all...
     // 具体的执行逻辑
      mav = invokeHandlerMethod(request, response, handlerMethod);
   }
	// 处理缓存
   if (!response.containsHeader(HEADER_CACHE_CONTROL)) {
      if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
         applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
      }
      else {
         prepareResponse(response);
      }
   }

   return mav;
}
invokeHandlerMethod
java 复制代码
protected ModelAndView invokeHandlerMethod(HttpServletRequest request,
      HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {

   ServletWebRequest webRequest = new ServletWebRequest(request, response);
   try {
     // 将注解@InitBinder的方法找出来
      WebDataBinderFactory binderFactory = getDataBinderFactory(handlerMethod);
     // 用来处理Model的,在处理器具体处理之前对Model进行初始化,在处理万请求之后对Model参数进行更新
      ModelFactory modelFactory = getModelFactory(handlerMethod, binderFactory);
		// 用于参数绑定、处理请求以及返回值处理
      ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(handlerMethod);
      invocableMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
      invocableMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
      invocableMethod.setDataBinderFactory(binderFactory);
      invocableMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
		// 用于保存Model和View的
      ModelAndViewContainer mavContainer = new ModelAndViewContainer();
     // 先将FlashMap中的数据添加进ModelAndViewContainer中
      mavContainer.addAllAttributes(RequestContextUtils.getInputFlashMap(request));
     // 初始化Model,处理@SessionAttributes注解和WebDataBinder定义的全局数据
      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();
         
         invocableMethod = invocableMethod.wrapConcurrentResult(result);
      }
			// 执行请求
      invocableMethod.invokeAndHandle(webRequest, mavContainer);
      if (asyncManager.isConcurrentHandlingStarted()) {
         return null;
      }
			// 请求执行完之后进行处理
     // ①modelFactory.updateModel 更新Model
     // ②根据mavContainer创建modelAndView
     // ③如果model是RedirectAttributes类型,需要设置到FlashMap中
      return getModelAndView(mavContainer, modelFactory, webRequest);
   }
   finally {
      webRequest.requestCompleted();
   }
}

zhhll.icu/2021/框架/spr...

本文由mdnice多平台发布

相关推荐
向阳121812 分钟前
Dubbo负载均衡
java·运维·负载均衡·dubbo
Gu Gu Study22 分钟前
【用Java学习数据结构系列】泛型上界与通配符上界
java·开发语言
WaaTong1 小时前
《重学Java设计模式》之 原型模式
java·设计模式·原型模式
m0_743048441 小时前
初识Java EE和Spring Boot
java·java-ee
AskHarries1 小时前
Java字节码增强库ByteBuddy
java·后端
小灰灰__1 小时前
IDEA加载通义灵码插件及使用指南
java·ide·intellij-idea
夜雨翦春韭1 小时前
Java中的动态代理
java·开发语言·aop·动态代理
程序媛小果2 小时前
基于java+SpringBoot+Vue的宠物咖啡馆平台设计与实现
java·vue.js·spring boot
追风林2 小时前
mac m1 docker本地部署canal 监听mysql的binglog日志
java·docker·mac
芒果披萨2 小时前
El表达式和JSTL
java·el