首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【springboot】关于ConditionalOnMissingBean/ConditionalOnBean判断失效问题

【springboot】关于ConditionalOnMissingBean/ConditionalOnBean判断失效问题

作者头像
master336
发布2026-06-15 19:31:34
发布2026-06-15 19:31:34
1040
举报

前提

此处不讨论实际Bean确实存在/不存在及存在多个的问题,仅讨论一种看似符合条件但按预期加载的情况。

情景复现

代码

代码语言:javascript
复制
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ RequestInterceptor.class, HttpServletRequest.class })
@ConditionalOnMissingBean(BdapRequestInterceptor.class)
@Slf4j
public class BdapRequestInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
    // do something
    }
  }

调整日志级别

此处调整主要用于观察bean加载过程,确认bean加载/不加载的的条件判断情况:

代码语言:javascript
复制
#以logback为例
logging:
  level:
    org: debug

观察日志:

··· BdapRequestInterceptor: Did not match: - @ConditionalOnMissingBean (types: com.pactera.bi.app.config.BdapRequestInterceptor; SearchStrategy: all) found beans of type ‘com.pactera.bi.app.config.BdapRequestInterceptor’ bdapRequestInterceptor (OnBeanCondition) Matched: - @ConditionalOnClass found required classes ‘feign.RequestInterceptor’, ‘javax.servlet.http.HttpServletRequest’ (OnClassCondition) - @ConditionalOnWebApplication (required) found ‘session’ scope (OnWebApplicationCondition) ···

在这里插入图片描述
在这里插入图片描述

分析与思考

  1. 从日志上来看,此时提示未生效的原因是因为@ConditionalOnMissingBean 的条件未满足,但是当服务启动完成时,容器中并没有BdapRequestInterceptor这个Bean。
  2. 技术面分析1:@Configuration我们经常用来定义Bean,对于@Configuration所注解的类来说,该配置类也会交由(配置)容器管理(AnnotationConfigApplicationContext),在@ConditionalOnMissingBean 判断BdapRequestInterceptor时Configuration已被提交配置容器。
  3. 技术面分析2: @Configuration种的Bean由注解@Bean负责声明, Configuration配置类交由ConfigurationClassPostProcessor(ConfigurationClassParser)负责进一步解析,与自动配置类种的解析并不在同一时间维度上。
  4. 技术面分析3: ConditionalOnMissingBean/ConditionalOnBean条件判断时,其并不会影响@Configuration种@Bean的初始化过程(比如优先/延迟声明等),也就是说当这个判断条件发挥作用时,所需决策的目标Bean可能受是否自动化配置、Order、PostBeanProcessor及BeanDefinitionRegistrar、BeanDefinition等因素的影响;
  5. 如果要消除其中的不确定性,应尽量避免其中的不确定性,比如此例种,可讲Bean改为自动化配置类(优先完成加载),或直接删除@Configuration(自动配置完成加载)标记。
在这里插入图片描述
在这里插入图片描述

那么,由于配置类解析顺序的不同,ConditionalOnMissingBean/ConditionalOnBean在作用不同的Bean及BeanDefinition上时,所产生的效果也并不同。

看看官方怎么说?

You need to be very careful about the order in which bean definitions are added, as these conditions are evaluated based on what has been processed so far. For this reason, we recommend using only @ConditionalOnBean and @ConditionalOnMissingBean annotations on auto-configuration classes (since these are guaranteed to load after any user-defined bean definitions have been added) @ConditionalOnBean and @ConditionalOnMissingBean do not prevent @Configuration classes from being created. The only difference between using these conditions at the class level and marking each contained @Bean method with the annotation is that the former prevents registration of the @Configuration class as a bean if the condition does not match

在这里插入图片描述
在这里插入图片描述

首先官方的建议是在自动配置种才建议使用ConditionalOnMissingBean/ConditionalOnBean注解,其次提醒了加载顺序的问题。 关于自动配置(此处仅引用,更为详细的建议自行查询官方资料):

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-06-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前提
  • 情景复现
    • 代码
    • 调整日志级别
    • 观察日志:
    • 分析与思考
    • 看看官方怎么说?
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档