【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个按键,…...
Leetcode 3576. Transform Array to All Equal Elements
Leetcode 3576. Transform Array to All Equal Elements 1. 解题思路2. 代码实现 题目链接:3576. Transform Array to All Equal Elements 1. 解题思路 这一题思路上就是分别考察一下是否能将其转化为全1或者全-1数组即可。 至于每一种情况是否可以达到…...
循环冗余码校验CRC码 算法步骤+详细实例计算
通信过程:(白话解释) 我们将原始待发送的消息称为 M M M,依据发送接收消息双方约定的生成多项式 G ( x ) G(x) G(x)(意思就是 G ( x ) G(x) G(x) 是已知的)࿰…...
无法与IP建立连接,未能下载VSCode服务器
如题,在远程连接服务器的时候突然遇到了这个提示。 查阅了一圈,发现是VSCode版本自动更新惹的祸!!! 在VSCode的帮助->关于这里发现前几天VSCode自动更新了,我的版本号变成了1.100.3 才导致了远程连接出…...
连锁超市冷库节能解决方案:如何实现超市降本增效
在连锁超市冷库运营中,高能耗、设备损耗快、人工管理低效等问题长期困扰企业。御控冷库节能解决方案通过智能控制化霜、按需化霜、实时监控、故障诊断、自动预警、远程控制开关六大核心技术,实现年省电费15%-60%,且不改动原有装备、安装快捷、…...
Golang dig框架与GraphQL的完美结合
将 Go 的 Dig 依赖注入框架与 GraphQL 结合使用,可以显著提升应用程序的可维护性、可测试性以及灵活性。 Dig 是一个强大的依赖注入容器,能够帮助开发者更好地管理复杂的依赖关系,而 GraphQL 则是一种用于 API 的查询语言,能够提…...
相机从app启动流程
一、流程框架图 二、具体流程分析 1、得到cameralist和对应的静态信息 目录如下: 重点代码分析: 启动相机前,先要通过getCameraIdList获取camera的个数以及id,然后可以通过getCameraCharacteristics获取对应id camera的capabilities(静态信息)进行一些openCamera前的…...
DBAPI如何优雅的获取单条数据
API如何优雅的获取单条数据 案例一 对于查询类API,查询的是单条数据,比如根据主键ID查询用户信息,sql如下: select id, name, age from user where id #{id}API默认返回的数据格式是多条的,如下: {&qu…...
BCS 2025|百度副总裁陈洋:智能体在安全领域的应用实践
6月5日,2025全球数字经济大会数字安全主论坛暨北京网络安全大会在国家会议中心隆重开幕。百度副总裁陈洋受邀出席,并作《智能体在安全领域的应用实践》主题演讲,分享了在智能体在安全领域的突破性实践。他指出,百度通过将安全能力…...
拉力测试cuda pytorch 把 4070显卡拉满
import torch import timedef stress_test_gpu(matrix_size16384, duration300):"""对GPU进行压力测试,通过持续的矩阵乘法来最大化GPU利用率参数:matrix_size: 矩阵维度大小,增大可提高计算复杂度duration: 测试持续时间(秒&…...
企业如何增强终端安全?
在数字化转型加速的今天,企业的业务运行越来越依赖于终端设备。从员工的笔记本电脑、智能手机,到工厂里的物联网设备、智能传感器,这些终端构成了企业与外部世界连接的 “神经末梢”。然而,随着远程办公的常态化和设备接入的爆炸式…...
