Spring Boot 3.x 新特性与使用详解(含注解实战)
一、Spring Boot 3.x 核心特性
1. Java 17+ 强制要求
- 最低版本:必须使用 JDK 17 或更高版本
- 现代语法支持:
Record
、Sealed Class
、Pattern Matching
// 使用 Record 定义 DTO
public record UserDTO(String name, int age) {
}
2. Jakarta EE 9+ 全面适配
- 包名迁移:所有
javax.*
→jakarta.*
(影响 Servlet、JPA、Validation 等) - 实战示例:
// JPA Entity 注解包名变化 import jakarta.persistence.Entity; import jakarta.persistence.Id; @Entity public class User { @Id private Long id; private String name; }
二、新特性详解与注解实战
1. AOT(Ahead-of-Time)与 Native Image
- 目标:通过 GraalVM 生成原生镜像,启动时间 <100ms
- 关键注解:
@NativeHint
@NativeHint(
options = "--enable-http",
types = @TypeHint(types = User.class)
)
@SpringBootApplication
public class NativeApp {
public static void main(