跳到主要内容 Spring Boot 3 升级至 4 完整迁移指南 | 极客日志
Java java
Spring Boot 3 升级至 4 完整迁移指南 Spring Boot 3 升级至 4 需先确认 JDK 版本(最低 17,推荐 21)并备份代码。主要步骤包括更新父工程版本、添加属性迁移工具检查废弃配置、适配第三方依赖及替换废弃 API(如 Jackson 3)。启用虚拟线程和声明式 HTTP 客户端可提升性能。测试阶段需验证编译启动及功能逻辑,处理依赖冲突与类找不到问题。最后可通过 GraalVM 原生镜像和分层 JAR 优化构建效率。
孤勇者 发布于 2026/3/15 更新于 2026/4/18 3 浏览升级前准备
升级前需做好充分准备,避免直接修改代码导致潜在问题。建议按步骤有序进行。
1. 检查当前环境
首先确认当前使用的版本;打开 pom.xml 或 build.gradle,查看 Spring Boot 版本号。如果是 3.x 系列,可以开始升级;如果是 2.x 或更老版本,建议先升级到 3.x,再考虑升级到 4。
< >
org.springframework.boot
spring-boot-starter-parent
3.2.0
parent
<groupId >
</groupId >
<artifactId >
</artifactId >
<version >
</version >
</parent >
2. JDK 版本要求 Spring Boot 4 要求最低 JDK 17,推荐使用 JDK 21。如果仍在使用 JDK 8 或 JDK 11,需要先升级 JDK。
3. 备份代码 升级前务必备份代码,以便出现问题时回滚。使用 Git 时,可先提交当前代码或创建新分支专门用于升级。
git checkout -b upgrade-to-spring-boot-4
git add .
git commit -m "准备升级到 Spring Boot 4"
依赖升级
1. 更新 Spring Boot 版本 将 Spring Boot 版本改为 4.0.0。Maven 和 Gradle 配置方式不同,分别说明如下。
<parent >
<groupId > org.springframework.boot</groupId >
<artifactId > spring-boot-starter-parent</artifactId >
<version > 4.0.0</version >
</parent >
<properties >
<java.version > 21</java.version >
</properties >
plugins {
id 'org.springframework.boot' version '4.0.0'
id 'io.spring.dependency-management' version '1.1.4'
}
java {
sourceCompatibility = '21'
targetCompatibility = '21'
}
2. 添加属性迁移工具 部分配置属性可能被废弃或改名。Spring Boot 提供了 spring-boot-properties-migrator 工具,可在启动时自动检查并提示废弃属性。
<dependency >
<groupId > org.springframework.boot</groupId >
<artifactId > spring-boot-properties-migrator</artifactId >
<scope > runtime</scope >
</dependency >
// Gradle 配置
dependencies {
runtimeOnly 'org.springframework.boot:spring-boot-properties-migrator'
}
启动应用后,该工具会在日志中提示废弃属性。升级完成后,记得移除该依赖。
3. 检查第三方依赖兼容性 Spring Boot 4 升级了 Spring Framework 至 7.0,部分第三方依赖可能尚未适配。需逐一检查是否有兼容版本。
<dependency >
<groupId > com.mysql</groupId >
<artifactId > mysql-connector-j</artifactId >
</dependency >
<dependency >
<groupId > org.springframework.boot</groupId >
<artifactId > spring-boot-starter-data-redis</artifactId >
</dependency >
<dependency >
<groupId > org.springframework.boot</groupId >
<artifactId > spring-boot-starter-amqp</artifactId >
</dependency >
建议访问各依赖官网确认是否发布兼容 Spring Boot 4 的版本。
代码修改
1. 废弃的 API 替换 Spring Boot 4 全面支持 Jackson 3,Jackson 2 的自动配置被标记为废弃。
@Configuration
public class JacksonConfig {
@Bean
public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder () {
return new Jackson2ObjectMapperBuilder ();
}
}
@Configuration
public class JacksonConfig {
@Bean
public JacksonObjectMapperBuilder jacksonObjectMapperBuilder () {
return new JacksonObjectMapperBuilder ();
}
}
2. 配置属性迁移 部分配置属性已改名或被废弃。启动时 spring-boot-properties-migrator 会提示具体变化。
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
3. 虚拟线程配置 Spring Boot 4 深度集成虚拟线程。若使用 JDK 21,可启用以提升并发性能。
spring:
threads:
virtual:
enabled: true
@Configuration
public class VirtualThreadConfig {
@Bean
public Executor virtualThreadExecutor () {
return Executors.newVirtualThreadPerTaskExecutor();
}
@Bean
public TomcatProtocolHandlerCustomizer<?> protocolHandlerVirtualThreadExecutorCustomizer() {
return protocolHandler -> {
protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
};
}
}
4. 声明式 HTTP 客户端 Spring Framework 7.0 引入声明式 HTTP 客户端,替代 RestTemplate 和 WebClient。
@Service
public class UserService {
@Autowired
private RestTemplate restTemplate;
public User getUser (Long id) {
return restTemplate.getForObject("http://api.example.com/users/" + id, User.class);
}
}
@HttpExchange(url = "http://api.example.com")
public interface UserClient {
@GetExchange("/users/{id}")
User getUser (@PathVariable Long id) ;
}
@Service
public class UserService {
@Autowired
private UserClient userClient;
public User getUser (Long id) {
return userClient.getUser(id);
}
}
测试和验证
1. 编译检查
mvn clean compile
./gradlew clean build
2. 启动应用
3. 功能测试 @SpringBootTest
class ApplicationTest {
@Test
void contextLoads () {
}
@Test
void testUserService () {
}
}
常见问题处理
1. 依赖冲突
mvn dependency:tree
./gradlew dependencies
<dependency >
<groupId > com.example</groupId >
<artifactId > some-library</artifactId >
<exclusions >
<exclusion >
<groupId > org.springframework</groupId >
<artifactId > spring-core</artifactId >
</exclusion >
</exclusions >
</dependency >
2. 配置属性找不到 若日志提示属性被废弃,按提示修改为新属性名或删除。
3. 类找不到 若报 ClassNotFoundException,检查依赖版本是否兼容 Spring Boot 4。
升级后的优化
1. 启用虚拟线程 JDK 21 环境下建议启用虚拟线程以大幅提升并发性能。
spring:
threads:
virtual:
enabled: true
2. 使用 GraalVM 原生镜像 Spring Boot 4 对 GraalVM 支持更完善,可尝试编译原生镜像以加快启动速度并减少内存占用。
mvn spring-boot:build-image
./gradlew bootBuildImage
3. 使用分层 JAR 启用分层 JAR 可将依赖层、资源层和业务层分开,优化 Docker 镜像构建效率。
<plugin >
<groupId > org.springframework.boot</groupId >
<artifactId > spring-boot-maven-plugin</artifactId >
<configuration >
<layers >
<enabled > true</enabled >
</layers >
</configuration >
</plugin >
总结 从 Spring Boot 3 升级到 4 的主要步骤包括:
检查环境 :确认 JDK 版本及当前 Spring Boot 版本。
备份代码 :使用 Git 创建分支或备份文件。
升级依赖 :更新 Spring Boot 版本,检查第三方依赖兼容性。
修改代码 :替换废弃 API,迁移配置属性。
测试验证 :完成编译、启动及功能测试。
优化配置 :启用虚拟线程,使用原生镜像等特性。
升级过程需耐心细致,遇到问题查阅文档或日志提示。升级完成后,虚拟线程和原生镜像等特性将带来明显的性能提升。
微信扫一扫,关注极客日志 微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
相关免费在线工具 Keycode 信息 查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online
Escape 与 Native 编解码 JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online
JavaScript / HTML 格式化 使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online
JavaScript 压缩与混淆 Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online
Base64 字符串编码/解码 将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
Base64 文件转换器 将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online