bigywater.tech
Spring · 68

--debug 条件报告:为什么这个 Bean 没生效

为什么我的 Bean 没有生效?怎么打开条件报告?怎么看懂 Positive 和 Negative?用户自己定义了 Bean 之后自动配置怎么变?这篇用 Spring Boot 4.0.8 的真实报告逐条读。

23 / 37
webmvc 类路径、非 Web 模式下 Positive matches 与 Negative matches 的项数
3 种
打开报告的方式:--debug、-Ddebug=true、调高 …autoconfigure.logging 日志级别
myMapper
用户定义 JsonMapper 后,报告里挡住 jacksonJsonMapper 的那个 Bean 名

打开

实测 · 本机执行输出
  不加参数:输出里有 "CONDITIONS EVALUATION REPORT" = False,输出共 3 行
  --debug:输出里有 "CONDITIONS EVALUATION REPORT" = True,输出共 283 行
  -Ddebug=true:输出里有报告 = True
  只调高 logging.level.org.springframework.boot.autoconfigure.logging=DEBUG:输出里有报告 = True

--debug 或调高一个日志器的级别。

结构

实测 · 本机执行输出
  Positive matches:23 项
  Negative matches:37 项
  Exclusions:0 项
  Unconditional classes:0 项

Positive、Negative、Exclusions、Unconditional 四部分。

原文

实测 · 本机执行输出
  [Positive matches] JacksonAutoConfiguration#jacksonJsonMapper:
       JacksonAutoConfiguration#jacksonJsonMapper matched:
          - @ConditionalOnMissingBean (types: tools.jackson.databind.json.JsonMapper; SearchStrategy: all) did not find any beans (OnBeanCondition)
  [Negative matches] WebMvcAutoConfiguration:
       WebMvcAutoConfiguration:
          Did not match:
             - not a servlet web application (OnWebApplicationCondition)
          Matched:
             - @ConditionalOnClass found required classes 'jakarta.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet', 'org.springframework.web.servlet.config.annotation.WebMvcConfigure
  [Negative matches] MessageSourceAutoConfiguration:
       MessageSourceAutoConfiguration:
          Did not match:
             - ResourceBundle did not find bundle with basename messages (MessageSourceAutoConfiguration.ResourceBundleCondition)
  [Negative matches] ProjectInfoAutoConfiguration#buildProperties:
       ProjectInfoAutoConfiguration#buildProperties:
          Did not match:
             - @ConditionalOnResource did not find resource '${spring.info.build.location:classpath:META-INF/build-info.properties}' (OnResourceCondition)
  [Negative matches] Main#myMapper:
       Main#myMapper:
          Did not match:
             - @ConditionalOnProperty (user.mapper) did not find property 'user.mapper' (OnPropertyCondition)

每个没生效的原因都有一句具体的话。

用户覆盖

实测 · 本机执行输出
  用户没定义 → [Positive matches]
       JacksonAutoConfiguration#jacksonJsonMapper matched:
          - @ConditionalOnMissingBean (types: tools.jackson.databind.json.JsonMapper; SearchStrategy: all) did not find any beans (OnBeanCondition)
       JacksonAutoConfiguration#jsonMapperBuilder matched:
  用户定义了 myMapper → [Negative matches]
       JacksonAutoConfiguration#jacksonJsonMapper:
          Did not match:
             - @ConditionalOnMissingBean (types: tools.jackson.databind.json.JsonMapper; SearchStrategy: all) found beans of type 'tools.jackson.databind.json.JsonMapper' myMapper (OnBeanCondition)

报告指出是哪个 Bean 挡住了自动配置。

代价

实测 · 本机执行输出
  不加 --debug:3 次进程总用时(ms)[649, 629, 640]
  --debug:3 次进程总用时(ms)[612, 618, 628]

本机上看不出启动变慢。

分步演示

条件报告
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64 / OpenJDK 21.0.12(Homebrew) / Spring Boot 4.0.8(Spring Framework 7.0.9、Tomcat 11.0.24) / Spring Cloud 2025.1.3;用 javac 编译、java 直接运行,依赖由 Maven 从 Maven Central 解析;数字每次运行会有小幅抖动。用 Spring Boot 4.0.8(spring-boot-starter-webmvc 类路径,非 Web 模式)的单个程序,每次启动一个新 JVM;报告原文取自标准输出;项数按缩进行数出来;第五节每组只测了 3 次,包含 JVM 启动,不是严格的性能测试
    • 没有验证:Actuator 的 /actuator/conditions 端点输出;Web 应用(SERVLET 类型)下的报告(这里是非 Web 模式,所以 Tomcat、WebMvc 都在 Negative 里);ConditionEvaluationReport 的程序化读取在 Boot 之外的用法;多模块项目里同名配置类的区分;日志级别对报告格式的影响
    京ICP备2026067502号-1京公网安备11010502063487号