• Spring Webflux HttpHandler源码整理


    HttpHandler的构造
    1. 自动启动配置类:HttpHandlerAutoConfiguration
      @Bean
      public HttpHandler httpHandler(ObjectProvider propsProvider) {
          HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(this.applicationContext).build();
          WebFluxProperties properties = propsProvider.getIfAvailable();
          if (properties != null && StringUtils.hasText(properties.getBasePath())) {
              Map handlersMap = Collections.singletonMap(properties.getBasePath(), httpHandler);
              return new ContextPathCompositeHandler(handlersMap);
          }
         return httpHandler;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    2. ApplicationContext.applicationContext
      获取spring容器中的WebHandler并保存到WebHttpHandlerBuilder中 (这里传入的WebHandler为DispatcherHandler实例)
      获取spring容器中的WebFilter并保存到WebHttpHandlerBuilder中
      获取spring容器中的WebExceptionHandler并保存到WebHttpHandlerBuilder中
      获取spring容器中的HttpHandlerDecoratorFactory并保存到WebHttpHandlerBuilder中
      获取spring容器中WebSessionManager并保存到WebHttpHandlerBuilder中
      ...
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    3. WebHttpHandlerBuilder#build构建HttpHandler
      public HttpHandler build() {
         WebHandler decorated = new FilteringWebHandler(this.webHandler, this.filters);
         decorated = new ExceptionHandlingWebHandler(decorated,  this.exceptionHandlers);
         HttpWebHandlerAdapter adapted = new HttpWebHandlerAdapter(decorated);
         adapted.setSessionManager(this.sessionManager);
         adapted.setCodecConfigurer(this.codecConfigurer);
         adapted.setLocaleContextResolver(this.localeContextResolver);
         adapted.setForwardedHeaderTransformer(this.forwardedHeaderTransformer);
         adapted.setApplicationContext(this.applicationContext);
         adapted.afterPropertiesSet();
         return (this.httpHandlerDecorator != null ? this.httpHandlerDecorator.apply(adapted) : adapted);
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    4. 实例化FilteringWebHandler
      public FilteringWebHandler(WebHandler handler, List filters) {
          this.delegate = handler;
          this.chain = new DefaultWebFilterChain(handler, filters);
      }
      
      • 1
      • 2
      • 3
      • 4
    5. 实例化DefaultWebFilterChain
      public DefaultWebFilterChain(WebHandler handler, List filters) {
           this.allFilters = Collections.unmodifiableList(filters);
           this.handler = handler;
           DefaultWebFilterChain chain = initChain(filters, handler);
           this.currentFilter = chain.currentFilter;
           this.chain = chain.chain;
       }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    6. initChain代码片段
       private static DefaultWebFilterChain initChain(List filters, WebHandler handler) {
           DefaultWebFilterChain chain = new DefaultWebFilterChain(filters, handler, null, null);
           ListIterator iterator = filters.listIterator(filters.size());
           while (iterator.hasPrevious()) {
               chain = new DefaultWebFilterChain(filters, handler, iterator.previous(), chain);
           }
           return chain;
       }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
    HttpHandler的执行
    1. HttpWebHandlerAdapter#handle执行
      public Mono handle(ServerHttpRequest request, ServerHttpResponse response) {
         ServerWebExchange exchange = createExchange(request, response);
         return getDelegate().handle(exchange)
             .doOnSuccess(aVoid -> logResponse(exchange))
             .onErrorResume(ex -> handleUnresolvedError(exchange, ex))
             .then(Mono.defer(response::setComplete));
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    2. ExceptionHandlingWebHandler#handle(exchange)执行
      public Mono handle(ServerWebExchange exchange) {
           Mono completion;
           try {
               completion = super.handle(exchange);
           }catch (Throwable ex) {
               completion = Mono.error(ex);
           }
           for (WebExceptionHandler handler : this.exceptionHandlers) {
               completion = completion.onErrorResume(ex -> handler.handle(exchange, ex));
           }
           return completion;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    3. FilteringWebHandler#handle
      public Mono handle(ServerWebExchange exchange) {
        return this.chain.filter(exchange);
      }
      
      • 1
      • 2
      • 3
    4. DefaultWebFilterChain#filter执行
       public Mono filter(ServerWebExchange exchange) {
           return Mono.defer(() ->
                this.currentFilter != null && this.chain != null ?
                     invokeFilter(this.currentFilter, this.chain, exchange) :
                     this.handler.handle(exchange));
       }
       private Mono invokeFilter(WebFilter current, DefaultWebFilterChain chain, ServerWebExchange exchange) {
           String currentName = current.getClass().getName();
           return current.filter(exchange, chain).checkpoint(currentName + " [DefaultWebFilterChain]");
       }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    5. DispatcherHandler#handle执行
  • 相关阅读:
    Edge扩展插件推荐专业视频下载器
    eBPF书籍和教程良心推荐
    5.k8s jenkins集成k8s一键发布案例
    基础框架 Spring
    Angular tsconfig.json 文件里的 paths 用途
    系统分屏后录音机向左右滑动报错问题
    基于深度学习的端到端自动驾驶的最新进展:调研综述
    Python股票量化投资课学习记录1
    基于 webpack 5 实现自定义 loader
    Vue配置代理
  • 原文地址:https://blog.csdn.net/yichengjie_c/article/details/133517392