当前位置: 首页 > news >正文

Spring Webflux HttpHandler源码整理

HttpHandler的构造
  1. 自动启动配置类:HttpHandlerAutoConfiguration
    @Bean
    public HttpHandler httpHandler(ObjectProvider<WebFluxProperties> propsProvider) {HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(this.applicationContext).build();WebFluxProperties properties = propsProvider.getIfAvailable();if (properties != null && StringUtils.hasText(properties.getBasePath())) {Map<String, HttpHandler> handlersMap = Collections.singletonMap(properties.getBasePath(), httpHandler);return new ContextPathCompositeHandler(handlersMap);}return httpHandler;
    }
    
  2. ApplicationContext.applicationContext
    获取spring容器中的WebHandler并保存到WebHttpHandlerBuilder中 (这里传入的WebHandler为DispatcherHandler实例)
    获取spring容器中的WebFilter并保存到WebHttpHandlerBuilder中
    获取spring容器中的WebExceptionHandler并保存到WebHttpHandlerBuilder中
    获取spring容器中的HttpHandlerDecoratorFactory并保存到WebHttpHandlerBuilder中
    获取spring容器中WebSessionManager并保存到WebHttpHandlerBuilder中
    ...
    
  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);
    }
    
  4. 实例化FilteringWebHandler
    public FilteringWebHandler(WebHandler handler, List<WebFilter> filters) {this.delegate = handler;this.chain = new DefaultWebFilterChain(handler, filters);
    }
    
  5. 实例化DefaultWebFilterChain
    public DefaultWebFilterChain(WebHandler handler, List<WebFilter> filters) {this.allFilters = Collections.unmodifiableList(filters);this.handler = handler;DefaultWebFilterChain chain = initChain(filters, handler);this.currentFilter = chain.currentFilter;this.chain = chain.chain;}
    
  6. initChain代码片段
     private static DefaultWebFilterChain initChain(List<WebFilter> filters, WebHandler handler) {DefaultWebFilterChain chain = new DefaultWebFilterChain(filters, handler, null, null);ListIterator<? extends WebFilter> iterator = filters.listIterator(filters.size());while (iterator.hasPrevious()) {chain = new DefaultWebFilterChain(filters, handler, iterator.previous(), chain);}return chain;}
    
HttpHandler的执行
  1. HttpWebHandlerAdapter#handle执行
    public Mono<Void> 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));
    }
    
  2. ExceptionHandlingWebHandler#handle(exchange)执行
    public Mono<Void> handle(ServerWebExchange exchange) {Mono<Void> 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;
    }
    
  3. FilteringWebHandler#handle
    public Mono<Void> handle(ServerWebExchange exchange) {return this.chain.filter(exchange);
    }
    
  4. DefaultWebFilterChain#filter执行
     public Mono<Void> filter(ServerWebExchange exchange) {return Mono.defer(() ->this.currentFilter != null && this.chain != null ?invokeFilter(this.currentFilter, this.chain, exchange) :this.handler.handle(exchange));}private Mono<Void> invokeFilter(WebFilter current, DefaultWebFilterChain chain, ServerWebExchange exchange) {String currentName = current.getClass().getName();return current.filter(exchange, chain).checkpoint(currentName + " [DefaultWebFilterChain]");}
    
  5. DispatcherHandler#handle执行
http://www.lryc.cn/news/185800.html

相关文章:

  • Qt扩展-Advanced-Docking 简介及配置
  • Decorator
  • 分布式文件系统HDFS(林子雨慕课课程)
  • CSS中:root伪类的使用
  • VulnHub JANGOW
  • OpenMesh 获取网格面片各个顶点
  • 【前端设计模式】之原型模式
  • 软件设计原则
  • 【面试HOT100】哈希双指针滑动窗口
  • Ubuntu20.04 配置 yolov5_ros 功能包记录
  • Flink的处理函数——processFunction
  • Linux系统中的ps命令详解及用法介绍
  • 机器学习笔记 - 基于pytorch、grad-cam的计算机视觉的高级可解释人工智能
  • Python 编程基础 | 第五章-类与对象 | 5.1、定义类
  • 合宙Air780e+luatos+腾讯云物联网平台完成设备通信与控制(属性上报+4G远程点灯)
  • c++系列之string的模拟实现
  • Spring的beanName生成器AnnotationBeanNameGenerator
  • FFmpeg 命令:从入门到精通 | ffmpeg 命令直播
  • A (1087) : DS单链表--类实现
  • 异常:找不到匹配的key exchange算法
  • Arcgis打开影像分析窗口没反应
  • Spring(JavaEE进阶系列1)
  • Flink状态管理与检查点机制
  • 【threejs】基本编程概念及海岛模型展示逻辑
  • python小技巧:创建单链表及删除元素
  • ADuM1250 ADuM1251 模块 I2C IIC总线2500V电磁隔离 接口保护
  • C# 把多个dll合成一个dll
  • scipy.sparse.coo_matrix.sum()关于axis的用法
  • C++类与对象(下)
  • SpringBoot——》引入Redis