摘要:本文介绍了Spring Boot的基础使用。
环境
Windows 10 企业版 LTSC 21H2
Java 1.8
Maven 3.6.3
Spring 5.3.31
Spring Boot 2.7.18
1 项目创建
1.1 创建方式
在IDEA中创建Spring Boot项目有以下方式:
- 通过Spring Initializr创建项目,这种方式最简单,也最推荐,但不支持使用过时的版本。
- 通过Maven创建项目,这种方式最原始,也最灵活,可以使用任何版本。
1.2 项目结构
典型的Spring Boot项目结构如下:
code1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| demo-springboot ├── src/ │ ├── main/ │ │ ├── java/ │ │ │ └── com/ │ │ │ └── example/ │ │ │ ├── controller/ │ │ │ │ └── DemoController.java │ │ │ └── DemoApplication.java │ │ └── resources/ │ │ ├── application.properties │ │ ├── static/ │ │ └── templates/ │ └── test/ ├── target/ └── pom.xml
|
2 快速入门
2.1 配置依赖
配置Maven依赖:
pom.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.18</version> <relativePath/> </parent>
<groupId>com.example</groupId> <artifactId>demo-springboot</artifactId> <version>1.0.0-SNAPSHOT</version>
<properties> <java.version>1.8</java.version> </properties>
<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> </project>
|
2.2 启动类
将带有@SpringBootApplication注解的类作为Spring Boot应用的启动类:
java1 2 3 4 5 6
| @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
|
2.3 业务类
创建业务类:
java1 2 3 4 5 6 7
| @RestController public class DemoController { @GetMapping("/demo") public String demo() { return "欢迎"; } }
|
2.4 启动应用
执行启动类会在控制台输出Spring Boot的启动日志:
log1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.18)
2026-03-02 09:03:52.338 INFO 32488 --- [ main] com.example.DemoApplication : Starting DemoApplication using Java 1.8.0_201 on ... 2026-03-02 09:03:52.345 INFO 32488 --- [ main] com.example.DemoApplication : No active profile set, falling back to 1 default profile: "default" 2026-03-02 09:03:53.064 INFO 32488 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2026-03-02 09:03:53.071 INFO 32488 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2026-03-02 09:03:53.071 INFO 32488 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.83] 2026-03-02 09:03:53.157 INFO 32488 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2026-03-02 09:03:53.157 INFO 32488 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 745 ms 2026-03-02 09:03:53.402 INFO 32488 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2026-03-02 09:03:53.410 INFO 32488 --- [ main] com.example.DemoApplication : Started DemoApplication in 1.481 seconds (JVM running for 2.371)
|
Spring Boot内置了Tomcat服务器,启动后可以通过http://localhost:8080/demo访问。
2.5 打包部署
在pom.xml中添加Maven插件以后,可以通过mvn package命令将应用打包为可执行的JAR文件。
进入target目录后,在CMD命令行中执行以下命令:
cmd1
| java -jar demo-springboot-1.0.0-SNAPSHOT.jar
|
启动后会在窗口中输出日志,并且可以通过http://localhost:8080/demo访问。
3 开发工具
使用开发工具可以实现代码热部署,无需重新启动应用。
导入依赖:
pom.xml1 2 3 4 5 6
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
|
条