第一个 Spring Boot 应用
约 1160 字大约 4 分钟
布欧-Lewyon
2026-05-15
首页 › Spring Boot › 快速入门 › 第一个 Spring Boot 应用
本节在 JDK 17+ 与 Spring Boot 3.x 下,创建一个可运行的最小 Web 应用,理解 @SpringBootApplication 与内嵌容器的运行方式。
创建工程
推荐使用 Spring Initializr 或 IDE(IntelliJ IDEA Ultimate / VS Code + Extension Pack)生成项目骨架:
- Project:Maven(或 Gradle)
- Language:Java
- Spring Boot:3.x(最新稳定版)
- Group:
com.example - Artifact:
demo - Dependencies:
Spring Web
生成后下载并解压,项目结构如下:
demo/
├── pom.xml
├── src/
│ ├── main/
│ │ ├── java/com/example/demo/
│ │ │ └── DemoApplication.java
│ │ └── resources/
│ │ ├── application.properties
│ │ └── static/ (空目录)
│ │ └── templates/ (空目录)
│ └── test/java/com/example/demo/
│ └── DemoApplicationTests.java父 POM 与起步依赖
Maven 项目的 pom.xml 关键片段:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.1</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>spring-boot-starter-parent 管理了所有依赖版本,子模块引入 starter 时无需写 version。spring-boot-starter-web 包含内嵌 Tomcat、Spring MVC 与 Jackson。
入口类
自动生成的入口类如下:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}@SpringBootApplication 是三个注解的合成:
@SpringBootConfiguration:标记此类为配置类(等价于@Configuration)。@EnableAutoConfiguration:开启自动配置——Spring Boot 根据 classpath 上的 jar 推断需要装配的 Bean。@ComponentScan:扫描当前包及其子包中的@Component/@Service/@Controller/@Repository。
添加一个 REST 端点
验证应用是否正常运行,可以在入口类所在包下新建一个 Controller:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot 3!";
}
}启动与验证
# 使用 Maven
./mvnw spring-boot:run
# 或打包后运行
./mvnw clean package -DskipTests
java -jar target/demo-0.0.1-SNAPSHOT.jar启动日志末尾可见:
Tomcat started on port 8080 (http)
Started DemoApplication in 1.5 seconds访问 http://localhost:8080/hello,返回:
Hello, Spring Boot 3!生产意识:生产环境务必指定启动 Profile(
--spring.profiles.active=prod),否则默认使用空 Profile,可能暴露 H2 控制台或过低日志级别的配置。不要依赖-DskipTests跳过测试——建立 CI 流水线后应由流水线区分阶段控制。
进阶阅读:SpringApplication 启动生命周期
初学者可以先只关注"能运行起来",之后再来了解内部原理。
SpringApplication.run() 是 Spring Boot 应用的启动枢纽,其内部流程可以分为 8 个阶段:
其中第 ⑥ 步 `onRefresh()` 是内嵌 Web 容器的启动时机——`ServletWebServerApplicationContext.onRefresh()` 会调用 `createWebServer()` → 工厂创建 Tomcat/Jetty/Undertow → 注册 `DispatcherServlet` → 启动容器。在第 ⑧ 步 `ApplicationReadyEvent` 发布前,应用对外端口已经打开,但尚未运行 `CommandLineRunner`,这是设计上允许在启动后置处理完成前接收请求——如果后置处理依赖外部资源(如预热缓存),需要考虑延迟就绪探针。
> **生产意识**:了解启动阶段对性能调优至关重要——第 ⑥ 步的 `refresh()` 是整个启动过程中最耗时的部分,其中 `invokeBeanFactoryPostProcessors`(处理自动配置)和 `finishBeanFactoryInitialization`(实例化所有单例)是两大瓶颈。如需优化启动速度,关注点应是:①减少自动配置类的数量(`spring.autoconfigure.exclude`);②将非关键 Bean 改为延迟初始化(`@Lazy`);③开启 Spring AOT(Boot 3.x+)或 CDS(Boot 4.x+ 开箱支持)。
## 默认端口与上下文路径
- 默认端口:**8080**
- 上下文路径(context path):**空串**(即 `/hello` 而非 `/demo/hello`)
如需修改,在 `application.properties` 中:
```properties
server.port=9090
server.servlet.context-path=/demo小结
- Spring Initializr 是推荐的起点,选择
spring-boot-starter-web即可获得完整 Web 能力。 @SpringBootApplication开启自动配置和组件扫描。SpringApplication.run()启动内嵌 Tomcat,应用无需外部容器。- 默认端口 8080,上下文路径为空。
- 易错:
@SpringBootApplication默认只扫描其所在包及其子包。若入口类放在com.example而 Controller 放在com.other,则扫描不到。如需自定义扫描路径,使用@SpringBootApplication(scanBasePackages = "com.example")。 - 思考任务:在 Initializr 上勾选
Spring Web后下载,添加一个返回 JSON 的@GetMapping端点,启动后验证返回内容。
上一节:Spring Boot 概述
下一节:推荐工程结构
