摘要:本文深入解析Spring Boot 3.0至3.5的核心更新,涵盖Java 17支持、GraalVM原生镜像、虚拟线程、Spring AI集成等关键特性,并提供实战迁移方案。
javax
迁移至jakarta
(Servlet/JPA等)java// 旧:javax.servlet.HttpServletRequest
// 新:jakarta.servlet.HttpServletRequest
bash./mvnw spring-boot:build-image -DskipTests
yaml# application.yml
spring:
docker:
compose:
enabled: true
file: docker-compose.yml
java@Testcontainers
@SpringBootTest
class MyIntegrationTest {
@Container
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15");
}
propertiesspring.threads.virtual.enabled=true
java@Autowired OpenAiChatClient chatClient;
String reply = chatClient.call("生成Spring Boot教程大纲");
javajdbcClient.sql("SELECT * FROM users")
.param(1, "Alice")
.query(Users.class);
java@ServiceConnection
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16");
java@Bean
OAuth2AuthorizedClientManager auth2Manager() {
return new AuthorizedClientServiceOAuth2AuthorizedClientManager(clientRegistrations, clients);
}
bash# 使用OpenRewrite一键迁移
mvn org.openrewrite.maven:rewrite-maven-plugin:run \
-Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-spring:LATEST
废弃组件 | 替代方案 |
---|---|
RestTemplate | RestClient / WebClient |
Jersey 支持 | 默认使用Spring MVC |
spring.config 配置树 | ConfigData API |
java// 添加Native Hints
@NativeHint(
options = "--enable-all-security-services"
)
public class MyNativeConfiguration {}
xml<!-- pom.xml -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.0</version>
</parent>
spring-boot-migrator
工具扫描兼容性结语:Spring Boot 3.x系列标志着云原生Java应用的新纪元。拥抱变化,专注价值交付!
附录