bigywater.tech
Spring · 154

Boot 4 升级后依赖没变却悄悄失效:Flyway、Kafka、RestClient 缺什么就加哪个 starter

Spring Boot 4 把自动配置拆进了很多模块。只升版本号,哪些功能会悄悄消失?要补哪个 starter?这篇用 3.5 的依赖写法在 4.0.8 上逐个验证。

没有创建
Flyway 只放 flyway-core,换成 4.0.8 之后迁移脚本没执行、fw_t 表没有创建,没有任何报错
5 / 7
7 种常见依赖里在 4.0.8 上需要补 starter 才恢复的个数(AOP 与 Prometheus 不受影响)
Unsupported Database
用 spring-boot-starter-flyway 却没放 flyway-mysql 时的启动错误

Flyway

实测 · 本机执行输出
  依赖对照:3.5 写法 = starter-web + spring-boot-starter-jdbc + flyway-core + flyway-mysql + mysql-connector-j;4.0 写法 = starter-web + spring-boot-starter-jdbc + spring-boot-starter-flyway + flyway-mysql + mysql-connector-j
  3.5.16 + 3.5 写法:表 fw_t 已创建;org.flywaydb.core.Flyway → 有 Bean [flyway]
  4.0.8 + 3.5 写法(只换版本):表 fw_t 没有创建;org.flywaydb.core.Flyway → 类在 classpath 上,但容器里没有这个 Bean
  4.0.8 + 4.0 的 starter:表 fw_t 已创建;org.flywaydb.core.Flyway → 有 Bean [flyway]
  4.0.8 + 4.0 的 starter,但不放 flyway-mysql:启动失败 Caused by: org.flywaydb.core.api.FlywayException: Unsupported Database: MySQL 26.10

只放 flyway-core 在 4.0.8 里迁移静默不执行。

Liquibase

实测 · 本机执行输出
  依赖对照:3.5 写法 = starter-web + spring-boot-starter-jdbc + liquibase-core + mysql-connector-j;4.0 写法 = starter-web + spring-boot-starter-jdbc + spring-boot-starter-liquibase + mysql-connector-j
  3.5.16 + 3.5 写法:表 lb_t 已创建;liquibase.integration.spring.SpringLiquibase → 有 Bean [liquibase]
  4.0.8 + 3.5 写法(只换版本):表 lb_t 没有创建;liquibase.integration.spring.SpringLiquibase → 类在 classpath 上,但容器里没有这个 Bean
  4.0.8 + 4.0 的 starter:表 lb_t 已创建;liquibase.integration.spring.SpringLiquibase → 有 Bean [liquibase]

同样静默失效,要加 spring-boot-starter-liquibase。

AOP

实测 · 本机执行输出
  依赖对照:3.5 写法 = starter-web + spring-boot-starter-aop;4.0 写法 = starter-web + spring-boot-starter-aspectj
  3.5.16 + 3.5 写法:GET /hello → hello+advised
  4.0.8 + 3.5 写法(只换版本):GET /hello → hello+advised
  4.0.8 + 4.0 的 starter:GET /hello → hello+advised

starter-aop 没了,但 aspectjweaver 单放仍生效。

Prometheus

实测 · 本机执行输出
  依赖对照:3.5 写法 = starter-web + spring-boot-starter-actuator + micrometer-registry-prometheus;4.0 写法 = starter-web + spring-boot-starter-actuator + micrometer-registry-prometheus + spring-boot-starter-micrometer-metrics
  3.5.16 + 3.5 写法:GET /actuator/prometheus → 200;GET /actuator/metrics → 200;io.micrometer.core.instrument.MeterRegistry → 有 Bean [prometheusMeterRegistry]
  4.0.8 + 3.5 写法(只换版本):GET /actuator/prometheus → 200;GET /actuator/metrics → 200;io.micrometer.core.instrument.MeterRegistry → 有 Bean [prometheusMeterRegistry]
  4.0.8 + 4.0 的 starter:GET /actuator/prometheus → 200;GET /actuator/metrics → 200;io.micrometer.core.instrument.MeterRegistry → 有 Bean [prometheusMeterRegistry]

actuator 加 micrometer-registry-prometheus 在 4.0.8 里照常工作。

Kafka

实测 · 本机执行输出
  依赖对照:3.5 写法 = starter-web + spring-kafka;4.0 写法 = starter-web + spring-boot-starter-kafka
  3.5.16 + 3.5 写法:org.springframework.kafka.core.KafkaTemplate → 有 Bean [kafkaTemplate]
  4.0.8 + 3.5 写法(只换版本):org.springframework.kafka.core.KafkaTemplate → 类在 classpath 上,但容器里没有这个 Bean
  4.0.8 + 4.0 的 starter:org.springframework.kafka.core.KafkaTemplate → 有 Bean [kafkaTemplate]

只放 spring-kafka 在 4.0.8 里没有 KafkaTemplate Bean。

RestClient

实测 · 本机执行输出
  依赖对照:3.5 写法 = starter-web + (无额外依赖);4.0 写法 = starter-web + spring-boot-starter-restclient
  3.5.16 + 3.5 写法:org.springframework.web.client.RestClient$Builder → 有 Bean [restClientBuilder] / org.springframework.boot.web.client.RestTemplateBuilder → 有 Bean [restTemplateBuilder] / org.springframework.boot.restclient.RestTemplateBuilder → 类不在 classpath 上
  4.0.8 + 3.5 写法(只换版本):org.springframework.web.client.RestClient$Builder → 类在 classpath 上,但容器里没有这个 Bean / org.springframework.boot.web.client.RestTemplateBuilder → 类不在 classpath 上 / org.springframework.boot.restclient.RestTemplateBuilder → 类不在 classpath 上
  4.0.8 + 4.0 的 starter:org.springframework.web.client.RestClient$Builder → 有 Bean [restClientBuilder] / org.springframework.boot.web.client.RestTemplateBuilder → 类不在 classpath 上 / org.springframework.boot.restclient.RestTemplateBuilder → 有 Bean [restTemplateBuilder]

starter-web 不再带 RestClient.Builder 的自动配置,RestTemplateBuilder 换了包名。

WebClient

实测 · 本机执行输出
  依赖对照:3.5 写法 = starter-web + spring-webflux;4.0 写法 = starter-web + spring-boot-starter-webclient
  3.5.16 + 3.5 写法:org.springframework.web.reactive.function.client.WebClient$Builder → 有 Bean [webClientBuilder]
  4.0.8 + 3.5 写法(只换版本):org.springframework.web.reactive.function.client.WebClient$Builder → 类在 classpath 上,但容器里没有这个 Bean
  4.0.8 + 4.0 的 starter:org.springframework.web.reactive.function.client.WebClient$Builder → 有 Bean [webClientBuilder]

spring-webflux 单放没有 WebClient.Builder Bean。

分步演示

starter
本步的对象

    本文用到的工具与边界

    • 本机 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 3.5.16 与 4.0.8;每种情况是 starter-web 的最小应用,/probe 报告对应类型的 Bean 有没有;Flyway 与 Liquibase 用本机自己起的 MySQL 实例,每次重建库,以表是否被创建为准;AOP 用一个 @Around 切面,Prometheus 访问 actuator 端点)
    • 没有验证:Redis、Cache、Session、Quartz、Mail、Thymeleaf 等其他依赖在 4.0.8 上只放底层库时是否也静默失效;Gradle 构建的传递依赖差异;spring-boot-starter-classic 在这些场景里是否能代替补 starter(只在 x148 里验证了它对编译没有帮助);测试 starter 拆分后的缺类情况(例如 spring-boot-starter-webmvc-test);多个 starter 之间的版本冲突
    京ICP备2026067502号-1京公网安备11010502063487号