免费开源终极指南:3分钟学会CompressO高效压缩视频与图片
2026/5/24 13:01:17
目录
一、核心优势
二、快速入门(创建第一个 Spring Boot 项目)
1. 环境准备
2. 创建项目(3 种方式)
方式 1:Spring Initializr(官方脚手架)
方式 2:IDEA 直接创建
方式 3:手动搭建(了解核心结构)
3. 编写核心代码
主启动类(必须)
控制器(测试接口)
4. 运行与访问
三、核心配置
1. 配置文件类型
2. 基础配置示例(application.yml)
3. 读取配置的方式
方式 1:@Value 注解
方式 2:@ConfigurationProperties(批量读取)
四、常用功能模块
五、打包与部署
1. 打包为可执行 JAR
2. 部署到服务器
六、关键注解说明
七、常见问题与解决方案
Spring Boot 是由 Pivotal 团队开发的基于 Spring 框架的快速开发脚手架,核心目标是简化 Spring 应用的搭建、配置和部署,通过 “约定优于配置” 的理念,让开发者专注于业务逻辑而非繁琐的配置。
spring-boot-starter-web自动配置 MVC、Tomcat 等);spring-boot-starter-data-jpa、spring-boot-starter-test),简化 pom.xml/gradle 配置;java -jar运行;Spring Web(Web 开发);xml
<!-- pom.xml 核心依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <relativePath/> </parent> <dependencies> <!-- Web 起步依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 测试依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!-- 打包为可执行 JAR --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>java
运行
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; // 核心注解:组合了 @Configuration + @EnableAutoConfiguration + @ComponentScan @SpringBootApplication public class DemoApplication { public static void main(String[] args) { // 启动 Spring Boot 应用 SpringApplication.run(DemoApplication.class, args); } }java
运行
package com.example.demo.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; // 组合 @Controller + @ResponseBody,返回 JSON/字符串而非视图 @RestController public class HelloController { @GetMapping("/hello") public String hello() { return "Hello Spring Boot!"; } }DemoApplication类的main方法;http://localhost:8080/hello,即可看到返回结果。Spring Boot 支持多种配置文件,优先级:properties<yml<yaml(推荐 yml,更简洁)。
src/main/resources/application.yml(或 application.properties);application-dev.yml、application-prod.yml。yaml
# 服务器配置 server: port: 8080 # 端口 servlet: context-path: /demo # 上下文路径 # Spring 配置 spring: # 多环境激活(dev/prod/test) profiles: active: dev # 数据源配置(以 MySQL 为例) datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai username: root password: 123456 # 自定义配置(可通过 @Value 或 @ConfigurationProperties 读取) custom: name: Spring Boot version: 3.2.0java
运行
@RestController public class ConfigController { // 读取自定义配置 @Value("${custom.name}") private String name; @Value("${custom.version}") private String version; @GetMapping("/config") public String getConfig() { return "Name: " + name + ", Version: " + version; } }java
运行
@Component @ConfigurationProperties(prefix = "custom") // 配置前缀 public class CustomConfig { private String name; private String version; // 生成 getter/setter public String getName() { return name; } public void setName(String name) { this.name = name; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } }Spring Boot 提供了丰富的 starter 依赖,覆盖主流开发场景:
| 功能场景 | 核心依赖(starter) |
|---|---|
| Web 开发 | spring-boot-starter-web |
| 数据库访问 | spring-boot-starter-data-jpa/MyBatis |
| 数据库连接 | spring-boot-starter-jdbc |
| 缓存 | spring-boot-starter-cache(+ Redis/Ehcache) |
| 安全认证 | spring-boot-starter-security |
| 消息队列 | spring-boot-starter-amqp(RabbitMQ) |
| 监控运维 | spring-boot-starter-actuator |
| 模板引擎 | spring-boot-starter-thymeleaf |
| 测试 | spring-boot-starter-test |
bash
运行
# Maven 打包(项目根目录执行) mvn clean package # 运行打包后的 JAR java -jar target/demo-0.0.1-SNAPSHOT.jar # 指定环境运行 java -jar target/demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=prodnohup java -jar demo.jar &后台运行(避免终端关闭停止);systemd配置成系统服务,实现开机自启。| 注解 | 作用 |
|---|---|
| @SpringBootApplication | 核心注解,组合 @Configuration + @EnableAutoConfiguration + @ComponentScan |
| @RestController | 标记控制器,返回 JSON / 字符串(替代 @Controller + @ResponseBody) |
| @GetMapping/@PostMapping | 简化请求映射(替代 @RequestMapping (method=RequestMethod.GET)) |
| @Autowired | 自动注入依赖(按类型) |
| @Component/@Service/@Repository | 标记组件,纳入 Spring 容器管理(分层注解) |
| @Configuration | 标记配置类(替代 XML 配置) |
| @Bean | 声明 Bean 实例,放入 Spring 容器 |
server.port为未占用端口(如 8081);mvn dependency:tree分析依赖,排除冲突包;application.yml拼写错误);spring-boot-devtools依赖,实现代码修改后自动重启。