【Spring Boot 源码学习】OnWebApplicationCondition 详解
Spring Boot 源码学习系列

OnWebApplicationCondition 详解
- 引言
- 往期内容
- 主要内容
- 1. getOutcomes 方法
- 2. getMatchOutcome 方法
- 3. isWebApplication 方法
- 3.1 isServletWebApplication 方法
- 3.2 isReactiveWebApplication 方法
- 3.3 isAnyWebApplication 方法
- 总结
引言
上篇博文带大家从 Spring Boot 源码深入详解了 OnBeanCondition,那本篇也同样从源码入手,带大家深入了解 OnWebApplicationCondition 的过滤匹配实现。
往期内容
在开始本篇的内容介绍之前,我们先来看看往期的系列文章【有需要的朋友,欢迎关注系列专栏】:
| Spring Boot 源码学习 |
| Spring Boot 项目介绍 |
| Spring Boot 核心运行原理介绍 |
| 【Spring Boot 源码学习】@EnableAutoConfiguration 注解 |
| 【Spring Boot 源码学习】@SpringBootApplication 注解 |
| 【Spring Boot 源码学习】走近 AutoConfigurationImportSelector |
| 【Spring Boot 源码学习】自动装配流程源码解析(上) |
| 【Spring Boot 源码学习】自动装配流程源码解析(下) |
| 【Spring Boot 源码学习】深入 FilteringSpringBootCondition |
| 【Spring Boot 源码学习】OnClassCondition 详解 |
| 【Spring Boot 源码学习】OnBeanCondition 详解 |
主要内容
本篇我们重点详解 OnWebApplicationCondition 的实现,参见如下:

1. getOutcomes 方法
鉴于前面博文的了解,我们知道 OnWebApplicationCondition 也是 FilteringSpringBootCondition 的子类,所以这里同样也是从 getOutcomes 方法源码来分析【Spring Boot 2.7.9】:
@Order(Ordered.HIGHEST_PRECEDENCE + 20)
class OnWebApplicationCondition extends FilteringSpringBootCondition {// ...@Overrideprotected ConditionOutcome[] getOutcomes(String[] autoConfigurationClasses,AutoConfigurationMetadata autoConfigurationMetadata) {ConditionOutcome[] outcomes = new ConditionOutcome[autoConfigurationClasses.length];for (int i = 0; i < outcomes.length; i++) {String autoConfigurationClass = autoConfigurationClasses[i];if (autoConfigurationClass != null) {outcomes[i] = getOutcome(autoConfigurationMetadata.get(autoConfigurationClass, "ConditionalOnWebApplication"));}}return outcomes;}// ...
}
上述逻辑很容易理解,遍历自动配置数组 autoConfigurationClasses ,循环如下:
-
首先,从
autoConfigurationClasses中获取自动配置数据autoConfigurationClass; -
然后,调用
AutoConfigurationMetadata接口的get(String className, String key)方法来获取与autoConfigurationClass关联的名为"ConditionalOnWebApplication"的条件属性值【即应用类型枚举值】;应用类型枚举可以查看
@ConditionalOnWebApplication注解获取,如下所示:
-
最后,调用
getOutcome方法,并传入上述获取的应用类型枚举值type:

- 如果
type是SERVLET, 则判断org.springframework.web.context.support.GenericWebApplicationContext是否存在;
如果不存在,则返回一个未满足过滤匹配条件的ConditionOutcome对象【其中包含 did not find servlet web application classes 的信息 】。 - 如果
type是REACTIVE,则判断org.springframework.web.reactive.HandlerResult是否存在;
如果不存在,则返回一个未满足过滤匹配条件的ConditionOutcome对象【其中包含 did not find reactive web application classes 的信息 】。 - 如果
org.springframework.web.context.support.GenericWebApplicationContext不存在且org.springframework.web.reactive.HandlerResult也不存在,则返回一个未满足过滤匹配条件的ConditionOutcome对象【其中包含 did not find reactive or servlet web application classes 的信息 】。 - 如果都存在,则直接返回
null。
- 如果
2. getMatchOutcome 方法
同 OnClassCondition 一样,OnWebApplicationCondition 同样实现了 FilteringSpringBootCondition 的父类 SpringBootCondition 中的抽象方法 getMatchOutcome 方法。
有关
SpringBootCondition的介绍,这里不赘述了,请查看笔者的 【Spring Boot 源码学习】OnClassCondition 详解。
那么,我们进入 getMatchOutcome 方法中查看如下源码【Spring Boot 2.7.9】:
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {boolean required = metadata.isAnnotated(ConditionalOnWebApplication.class.getName());ConditionOutcome outcome = isWebApplication(context, metadata, required);if (required && !outcome.isMatch()) {return ConditionOutcome.noMatch(outcome.getConditionMessage());}if (!required && outcome.isMatch()) {return ConditionOutcome.noMatch(outcome.getConditionMessage());}return ConditionOutcome.match(outcome.getConditionMessage());
}
我们来分析一下相关逻辑:
-
首先,通过调用
AnnotatedTypeMetadata接口的isAnnotated方法,判断元数据中是否存在@ConditionalOnWebApplication注解【当应用程序为 Web 应用程序时,该条件注解用来匹配】。如果返回true,表示元数据中存在指定注解;否则,返回false。 -
然后,调用
isWebApplication方法来获取条件匹配结果outcome【有关内容查看 第 3 小节】; -
如果
required为true【即存在@ConditionalOnWebApplication注解】,并且 条件结果不匹配,则返回一个新的ConditionOutcome对象,标记为不匹配,并带有原始的消息。 -
如果
required为false【即不存在@ConditionalOnWebApplication注解】,并且 条件结果匹配,则同样返回一个新的ConditionOutcome对象,标记为不匹配,并带有原始的消息。 -
最后,上述两个条件判断都不满足,则将返回一个匹配的
ConditionOutcome对象,并带有原始的消息。
3. isWebApplication 方法
下面,我们进入 isWebApplication 方法中:
private ConditionOutcome isWebApplication(ConditionContext context, AnnotatedTypeMetadata metadata,boolean required) {switch (deduceType(metadata)) {case SERVLET:return isServletWebApplication(context);case REACTIVE:return isReactiveWebApplication(context);default:return isAnyWebApplication(context, required);}
}
上述的逻辑也很简单:
-
首先,通过
deduceType方法获取可获取的应用类型;查看其源码可知,如果存在@ConditionalOnWebApplication注解,则获取其对应的type属性;否则默认返回Type.ANY【即任何Web应用程序都将匹配】。private Type deduceType(AnnotatedTypeMetadata metadata) {Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnWebApplication.class.getName());if (attributes != null) {return (Type) attributes.get("type");}return Type.ANY; } -
如果是
Type.SERVLET,则调用isServletWebApplication方法返回条件匹配结果。 -
如果是
Type.REACTIVE,则调用isReactiveWebApplication方法返回条件匹配结果。 -
如果不是上述两个应用类型,则默认调用
isAnyWebApplication方法返回条件匹配结果。
3.1 isServletWebApplication 方法
我们直接翻看 isServletWebApplication 方法的源码,如下:
private ConditionOutcome isServletWebApplication(ConditionContext context) {ConditionMessage.Builder message = ConditionMessage.forCondition("");if (!ClassNameFilter.isPresent(SERVLET_WEB_APPLICATION_CLASS, context.getClassLoader())) {return ConditionOutcome.noMatch(message.didNotFind("servlet web application classes").atAll());}if (context.getBeanFactory() != null) {String[] scopes = context.getBeanFactory().getRegisteredScopeNames();if (ObjectUtils.containsElement(scopes, "session")) {return ConditionOutcome.match(message.foundExactly("'session' scope"));}}if (context.getEnvironment() instanceof ConfigurableWebEnvironment) {return ConditionOutcome.match(message.foundExactly("ConfigurableWebEnvironment"));}if (context.getResourceLoader() instanceof WebApplicationContext) {return ConditionOutcome.match(message.foundExactly("WebApplicationContext"));}return ConditionOutcome.noMatch(message.because("not a servlet web application"));
}
我们来详细分析一下:
- 首先,检查类加载器中是否存在
org.springframework.web.context.support.GenericWebApplicationContext?- 如果没有,那么将返回不匹配的结果,并附带消息
"did not find servlet web application classes"。
- 如果没有,那么将返回不匹配的结果,并附带消息
- 如果条件上下文
context中BeanFactory不为空,则获取所有注册的scope名称,并检查其中是否包含"session"。如果包含,则返回匹配的结果,并附带消息"found session scope"。 - 如果条件上下文
context中Environment是ConfigurableWebEnvironment的实例,则将返回匹配的结果,并附带消息"found ConfigurableWebEnvironment"。 - 如果条件上下文
context中ResourceLoader是WebApplicationContext的实例,那么将返回匹配的结果,并附带消息"found WebApplicationContext"。 - 如果上述的条件都不满足,则最后将返回不匹配的结果,并附带消息
"not a servlet web application"。
3.2 isReactiveWebApplication 方法
同样,我们也先来查看下 isReactiveWebApplication 方法的源码,如下:
private ConditionOutcome isReactiveWebApplication(ConditionContext context) {ConditionMessage.Builder message = ConditionMessage.forCondition("");if (!ClassNameFilter.isPresent(REACTIVE_WEB_APPLICATION_CLASS, context.getClassLoader())) {return ConditionOutcome.noMatch(message.didNotFind("reactive web application classes").atAll());}if (context.getEnvironment() instanceof ConfigurableReactiveWebEnvironment) {return ConditionOutcome.match(message.foundExactly("ConfigurableReactiveWebEnvironment"));}if (context.getResourceLoader() instanceof ReactiveWebApplicationContext) {return ConditionOutcome.match(message.foundExactly("ReactiveWebApplicationContext"));}return ConditionOutcome.noMatch(message.because("not a reactive web application"));
}
通过上述 isServletWebApplication 方法中的分析,我们可以很快总结下:
- 首先,检查类加载器中是否存在
org.springframework.web.reactive.HandlerResult?- 如果没有,那么将返回不匹配的结果,并附带消息
"did not find reactive web application classes"。
- 如果没有,那么将返回不匹配的结果,并附带消息
- 如果条件上下文
context中Environment是ConfigurableReactiveWebEnvironment的实例,则将返回匹配的结果,并附带消息"found ConfigurableReactiveWebEnvironment"。 - 如果条件上下文
context中ResourceLoader是ReactiveWebApplicationContext的实例,那么将返回匹配的结果,并附带消息"found ReactiveWebApplicationContext"。 - 如果上述的条件都不满足,则最后将返回不匹配的结果,并附带消息
"not a reactive web application"。
3.3 isAnyWebApplication 方法
还是一样,我们先来看看 isAnyWebApplication 方法的源码,如下:
private ConditionOutcome isAnyWebApplication(ConditionContext context, boolean required) {ConditionMessage.Builder message = ConditionMessage.forCondition(ConditionalOnWebApplication.class,required ? "(required)" : "");ConditionOutcome servletOutcome = isServletWebApplication(context);if (servletOutcome.isMatch() && required) {return new ConditionOutcome(servletOutcome.isMatch(), message.because(servletOutcome.getMessage()));}ConditionOutcome reactiveOutcome = isReactiveWebApplication(context);if (reactiveOutcome.isMatch() && required) {return new ConditionOutcome(reactiveOutcome.isMatch(), message.because(reactiveOutcome.getMessage()));}return new ConditionOutcome(servletOutcome.isMatch() || reactiveOutcome.isMatch(),message.because(servletOutcome.getMessage()).append("and").append(reactiveOutcome.getMessage()));
}
这里就更简单了,总结如下:
- 首先,通过调用 isServletWebApplication 方法获取条件匹配结果;
- 如果
Servlet Web应用程序的条件结果匹配并且required为true,则返回一个包含匹配状态和相关消息的ConditionOutcome对象。
- 如果
- 接着,通过调用 isReactiveWebApplication 方法获取条件匹配结果;
- 如果
Reactive Web应用程序的条件结果匹配并且required为true,则同样返回一个包含匹配状态和相关消息的ConditionOutcome对象。
- 如果
- 最后,如果上述两种情况都不满足 或者
required为false,则返回一个新的ConditionOutcome对象,它包含servletOutcome.isMatch() || reactiveOutcome.isMatch()的匹配状态 和servletOutcome与reactiveOutcome两者拼接的消息。
总结
本篇 Huazie 带大家从源码角度深入了解了自动配置过滤匹配子类 OnWebApplicationCondition ,至此 Spring Boot 中有关自动配置过滤匹配的三个实现已经介绍完毕,当然有关过滤匹配条件的内容还没结束,下一篇笔者将介绍 @Conditional 条件注解。
相关文章:
【Spring Boot 源码学习】OnWebApplicationCondition 详解
Spring Boot 源码学习系列 OnWebApplicationCondition 详解 引言往期内容主要内容1. getOutcomes 方法2. getMatchOutcome 方法3. isWebApplication 方法3.1 isServletWebApplication 方法3.2 isReactiveWebApplication 方法3.3 isAnyWebApplication 方法 总结 引言 上篇博文带…...
力扣之二分法
今天,学习了二分法,详细内容见代码随想录 (programmercarl.com),讲得十分好。 力扣之35. 搜索插入位置 - 力扣(LeetCode)。 class Solution { public:int searchInsert(vector<int>& nums, int target) {in…...
css图形化理解--扭曲函数skew()
transform: skewX(30deg);transform: skewY(45deg);transform: skew(30deg,45deg);transform: skewX(angleX);transform: skewY(angleY);transform: skew(angleX,angleY); 是CSS中的一个2D变换方法,它用于对元素沿X轴、Y轴进行倾斜变换。其中,angle表示倾…...
八、互联网技术——物联网
文章目录 一、智慧物联案例分析二、M2M技术三、数据保护综合案例分析一、智慧物联案例分析 智能物流是一种典型的物联网应用。一个物流仓储管理系统架构如下图所示: [问题1] 图中的三层功能:仓库物品识别、网络接入、物流管理中心,分别可对应到物联网基本架构中的哪一层? …...
聊聊MySQL的聚簇索引和非聚簇索引
文章目录 1. 索引的分类1. 存储结构维度2. 功能维度3. 列数维度4. 存储方式维度5. 更新方式维度 2. 聚簇索引2.1 什么是聚簇索引2.2 聚簇索引的工作原理 3. 非聚簇索引(MySQL官方文档称为Secondary Indexes)3.1 什么是非聚簇索引3.2 非聚簇索引的工作原理…...
python之subprocess模块详解
介绍 subprocess是Python 2.4中新增的一个模块,它允许你生成新的进程,连接到它们的 input/output/error 管道,并获取它们的返回(状态)码。 这个模块的目的在于替换几个旧的模块和方法。 那么我们到底该用哪个模块、哪个…...
第10讲:Vue组件的定义与注册
定义组件 1. 在程序的 components 目录下新建一个名为 Child.vue 的文件 2. 在文件内键入如下代码 <template><div>Child</div> </template> <script> export default {name: Child } </script>新建的 Child .vue 文件即为我们定义的组件…...
Pycharm操作git仓库 合并等
菜单 Git CommitPushUpdate ProjectPullFetchMergreRebase 查询 查询分支 查询本地所有分支 # 查询本地分支 git branch# 查询远程分支 git branch -rPycharm查看当前分支 步骤: Git->Branches 哈喽,大家好,我是[有勇气的牛排]&…...
Flink+Doris 实时数仓
Flink+Doris 实时数仓 Doris基本原理 Doris基本架构非常简单,只有FE(Frontend)、BE(Backend)两种角色,不依赖任何外部组件,对部署和运维非常友好。架构图如下 可以 看到Doris 的数仓架构十分简洁,不依赖 Hadoop 生态组件,构建及运维成本较低。 FE(Frontend)以 Java 语…...
windows 任务计划自动提交 笔记到github 、gitee
一、必须有个git仓库托管到git上。 这个就不用说了,自己在github或者码云上新建一个仓库就行了。 二、创建自动提交脚本 这个bat脚本是在windows环境下使用的。 注意:windows定时任务下 调用自动提交git前,必须先进入该git仓库目录&#x…...
闭包和装饰器
#闭包的作用 #全局变量有被修改的风险,代码在命名空间上不够干净整洁 #第一种,不使用闭包的场景 account_amount0 def atm(num,depositTrue):global account_amountif deposit:account_amountnumprint(f"存款:{num},账户余额…...
注册器模式
注册器模式 注册器模式(Registry Pattern)是一种设计模式,用于管理和维护对象的注册和检索。它允许您在运行时注册对象,并通过一个唯一的标识符或名称来检索这些对象。这种模式通常用于构建可扩展的、松耦合的系统,其…...
5SpringMVC处理Ajax请求携带的JSON格式(“key“:value)的请求参数
SpringMVC处理Ajax 参考文章数据交换的常见格式,如JSON格式和XML格式 请求参数的携带方式 浏览器发送到服务器的请求参数有namevalue&...(键值对)和{key:value,...}(json对象)两种格式 URL请求会将请求参数以键值对的格式拼接到请求地址后面,form表单的GET和POST请求会…...
学习笔记|ADC|NTC原理|测温程序|STC32G单片机视频开发教程(冲哥)|第十九集:ADC应用之NTC
文章目录 1.NTC的原理开发板上的NTC 2.NTC的测温程序编写3.实战小练总结课后练习 1.NTC的原理 NTC(Negative Temperature Coefficient)是指随温度上升电阻呈指数关系减小、具有负温度系数的热敏电阻现象和材料。该材料是利用锰、铜、硅、钴、铁、镍、锌…...
Redisson 集成SpringBoot 详解
一、引入依赖 <dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.23.5</version></dependency> redison-spring-boot-starter依赖于与最新版本的spring-boot兼容…...
C# 下载模板文件 Excel
后端代码 [HttpGet("DownloadExcel")]public async Task<dynamic> DownloadExcel(string tmplName){var _fileName "导入表模板.xlsx";var filePath "Files\DownLoad\";var NewFile Path.Combine(filePath, tmplName);var stream new…...
如何做好sop流程图?sop流程图用什么软件做?
5.如何做好sop流程图?sop流程图用什么软件做? 建立标准作业程序sop已经成为企业进步和发展的必经之路,不过,很多刚刚开始着手搭建sop的企业并不知道要如何操作,对于如何做sop流程图、用什么软件做sop流程图等问题充满…...
JAVA编程题-求矩阵螺旋值
螺旋类 package entity; /*** 打印数组螺旋值类*/ public class Spiral { // 数组行private int row; // 数组列private int col; // 行列数private int size; // 当前行索引private int rowIndex; // 当前列索引private int colIndex; // 行开始索引private int rowStart; //…...
Python--入门
标识符 标识符由字母,数字,下划线_组成 第一个字符不能是数字,必须是字母或下划线 标识符区分大小写 关键字 关键字即保留字,定义标识符时不能使用关键字,python中的关键字如下图 注释 python中的单行注释用 # 多行注…...
STM32复习笔记(二):GPIO
目录 (一)Demo流程 (二)工程配置 (三)代码部分 (四)外部中断(EXTI) (一)Demo流程 首先,板子上有4个按键,…...
日语AI面试高效通关秘籍:专业解读与青柚面试智能助攻
在如今就业市场竞争日益激烈的背景下,越来越多的求职者将目光投向了日本及中日双语岗位。但是,一场日语面试往往让许多人感到步履维艰。你是否也曾因为面试官抛出的“刁钻问题”而心生畏惧?面对生疏的日语交流环境,即便提前恶补了…...
React Native 开发环境搭建(全平台详解)
React Native 开发环境搭建(全平台详解) 在开始使用 React Native 开发移动应用之前,正确设置开发环境是至关重要的一步。本文将为你提供一份全面的指南,涵盖 macOS 和 Windows 平台的配置步骤,如何在 Android 和 iOS…...
鸿蒙DevEco Studio HarmonyOS 5跑酷小游戏实现指南
1. 项目概述 本跑酷小游戏基于鸿蒙HarmonyOS 5开发,使用DevEco Studio作为开发工具,采用Java语言实现,包含角色控制、障碍物生成和分数计算系统。 2. 项目结构 /src/main/java/com/example/runner/├── MainAbilitySlice.java // 主界…...
GitFlow 工作模式(详解)
今天再学项目的过程中遇到使用gitflow模式管理代码,因此进行学习并且发布关于gitflow的一些思考 Git与GitFlow模式 我们在写代码的时候通常会进行网上保存,无论是github还是gittee,都是一种基于git去保存代码的形式,这样保存代码…...
【JVM面试篇】高频八股汇总——类加载和类加载器
目录 1. 讲一下类加载过程? 2. Java创建对象的过程? 3. 对象的生命周期? 4. 类加载器有哪些? 5. 双亲委派模型的作用(好处)? 6. 讲一下类的加载和双亲委派原则? 7. 双亲委派模…...
论文阅读:LLM4Drive: A Survey of Large Language Models for Autonomous Driving
地址:LLM4Drive: A Survey of Large Language Models for Autonomous Driving 摘要翻译 自动驾驶技术作为推动交通和城市出行变革的催化剂,正从基于规则的系统向数据驱动策略转变。传统的模块化系统受限于级联模块间的累积误差和缺乏灵活性的预设规则。…...
通过MicroSip配置自己的freeswitch服务器进行调试记录
之前用docker安装的freeswitch的,启动是正常的, 但用下面的Microsip连接不上 主要原因有可能一下几个 1、通过下面命令可以看 [rootlocalhost default]# docker exec -it freeswitch fs_cli -x "sofia status profile internal"Name …...
Xela矩阵三轴触觉传感器的工作原理解析与应用场景
Xela矩阵三轴触觉传感器通过先进技术模拟人类触觉感知,帮助设备实现精确的力测量与位移监测。其核心功能基于磁性三维力测量与空间位移测量,能够捕捉多维触觉信息。该传感器的设计不仅提升了触觉感知的精度,还为机器人、医疗设备和制造业的智…...
云原生安全实战:API网关Envoy的鉴权与限流详解
🔥「炎码工坊」技术弹药已装填! 点击关注 → 解锁工业级干货【工具实测|项目避坑|源码燃烧指南】 一、基础概念 1. API网关 作为微服务架构的统一入口,负责路由转发、安全控制、流量管理等核心功能。 2. Envoy 由Lyft开源的高性能云原生…...
今日行情明日机会——20250609
上证指数放量上涨,接近3400点,个股涨多跌少。 深证放量上涨,但有个小上影线,相对上证走势更弱。 2025年6月9日涨停股主要行业方向分析(基于最新图片数据) 1. 医药(11家涨停) 代表标…...
