SpringBoot3 开发实战:核心功能与高级特性
SpringBoot3 作为 Spring 框架最新版本,提供自动配置、起步依赖等改进。内容涵盖项目搭建、YAML 配置、Web 开发整合、MyBatis 集成、热部署、定时任务、监控与部署、原理分析及新特性如 ProblemDetails 和原生镜像。同时介绍 Lombok 和 MyBatisPlus 等实用工具,适合快速复习与实战参考。

SpringBoot3 作为 Spring 框架最新版本,提供自动配置、起步依赖等改进。内容涵盖项目搭建、YAML 配置、Web 开发整合、MyBatis 集成、热部署、定时任务、监控与部署、原理分析及新特性如 ProblemDetails 和原生镜像。同时介绍 Lombok 和 MyBatisPlus 等实用工具,适合快速复习与实战参考。

SpringBoot3 在 SpringBoot2 的基础上进行了重大改进,主要特点包括:
| 工具 | 版本要求 |
|---|---|
| IDEA | 2021.2.1+ |
| Java | 17+ |
| Maven | 3.5+ |
| Tomcat | 10.0+ |
| Servlet | 5.0+ |
| GraalVM | Community 22.3+ |
| Native Build Tools | 0.9.19+ |
访问 start.spring.io 生成项目,选择合适版本和依赖。
在 IDEA 中选择"Spring Initializr",配置项目信息和依赖。
<!-- 父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
</parent>
<!-- 起步依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- 打包插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
src/main/java # Java 代码
└── 启动类 # 项目入口
src/main/resources # 配置和资源文件
├── static # 静态资源 (css, js, img)
├── templates # 模板文件 (Thymeleaf)
└── application.yml # 配置文件
src/test/java # 测试代码
pom.xml # Maven 配置
# 简单数据
email: [email protected]
# 对象数据
my1:
email: [email protected]
password: yibeigen
# 集合数据
city1:
- beijing
- shanghai
- tianjin
// @Value 方式
@Value("${email}")
private String email;
// @ConfigurationProperties 方式
@ConfigurationProperties(prefix = "user")
public class UserConfig {
private int id;
private String username;
// getters and setters
}
// 方式一:注解方式
@WebServlet("/first")
public class FirstServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) {
System.out.println("First Servlet");
}
}
// 方式二:配置类方式
@Configuration
public class ServletConfig {
@Bean
public ServletRegistrationBean getServletRegistrationBean() {
return new ServletRegistrationBean(new SecondServlet(), "/second");
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf 示例</title>
</head>
<body>
<h2 th:text="${msg}">默认值</h2>
<!-- 条件判断 -->
<div th:if="${sex} == '男'">性别:男</div>
<div th:if="${sex} == '女'">性别:女</div>
<!-- 循环遍历 -->
<table>
<tr th:each="user : ${users}">
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
</tr>
</table>
</body>
</html>
// Mapper 接口
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
@Select("select * from student where id = #{id}")
Student findById(int id);
}
# 配置文件
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql:///student?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.itbaizhan.springbootmybatis.pojo
添加 DevTools 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
@Component
public class MyTask {
@Scheduled(cron="0 0 2 * * ?") // 每天凌晨 2 点执行
public void task1() {
System.out.println("定时任务执行:" + new Date());
}
}
@SpringBootApplication
@EnableScheduling
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@Controller
public class ConsultController {
@Autowired
private StudentMapper studentMapper;
@RequestMapping("/student/findById")
@ResponseBody
public Student findById(Integer id) {
return studentMapper.findById(id);
}
}
添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置:
management:
endpoints:
web:
exposure:
include: '*'
访问端点:
/actuator/health - 健康检查/actuator/metrics - 系统指标/actuator/loggers - 日志管理<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>3.1.3</version>
</dependency>
spring.boot.admin.client.url=http://localhost:9090
# application-dev.yml
server:
port: 8080
# application-test.yml
server:
port: 8081
# application-prod.yml
server:
port: 80
运行时指定环境:
java -jar app.jar --spring.profiles.active=prod
SpringBoot 的起步依赖通过 spring-boot-starter-parent 实现版本管理和依赖传递。
@SpringBootApplication 注解等同于:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
处理异常的新方式,符合 RFC 7807 规范:
spring.mvc.problemdetails.enabled=true
使用 GraalVM 生成原生可执行文件:
<profiles>
<profile>
<id>native</id>
<properties>
<repackage.classifier>exec</repackage.classifier>
<native-buildtools.version>0.9.19</native-buildtools.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
简化 POJO 代码:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String username;
private String password;
}
增强 MyBatis 功能:
@TableName("tb_student")
public class Student extends Model<Student> {
@TableId(value = "sid", type = IdType.AUTO)
private Integer id;
@TableField("sname")
private String name;
// CRUD 操作 student.insert(); student.updateById(); student.selectById();
}


微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online
JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online
使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online
Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online