抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

摘要:无。

环境

Windows 10 企业版 LTSC 21H2
Java 1.8
Maven 3.6.3
Spring 5.3.31
Spring Boot 2.7.18

1 入门案例

1.1 创建项目

目录结构:

code
1
2
3
4
5
6
7
8
9
10
11
12
13
demo-spring-boot
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ ├── controller/
│ │ │ │ └── DemoController.java
│ │ │ └── DemoApplication.java
│ │ └── resources/
│ │ └── application.yml
│ └── test/
└── pom.xml

1.2 管理依赖

创建pom.xml文件:

pom.xml
1
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
38
<?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 http://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/> <!-- lookup parent from repository -->
</parent>

<groupId>com.example</groupId>
<artifactId>demo-spring-boot</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!-- Spring Boot Web -->
<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>

1.3 编写代码

创建DemoController类:

java
1
2
3
4
5
6
7
@RestController
public class DemoController {
@GetMapping("/demo")
public String demo() {
return "欢迎";
}
}

创建DemoApplication类,使用@SpringBootApplication注解表示启动类:

java
1
2
3
4
5
6
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

1.4 运行代码

执行启动类会在控制台输出Spring Boot的启动日志:

log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.18)

INFO 32488 --- [ main] com.example.DemoApplication : Starting DemoApplication using Java 1.8.0_201 on ...
INFO 32488 --- [ main] com.example.DemoApplication : No active profile set, falling back to 1 default profile: "default"
INFO 32488 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
INFO 32488 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
INFO 32488 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.83]
INFO 32488 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
INFO 32488 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 745 ms
INFO 32488 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
INFO 32488 --- [ main] com.example.DemoApplication : Started DemoApplication in 1.481 seconds (JVM running for 2.371)

依赖内嵌了Tomcat服务器,启动后可以直接访问http://localhost:8080/demo接口。

1.5 打包部署

在pom.xml文件中添加Maven插件以后,可以通过mvn package命令将应用打包为可执行的Jar文件。

进入target目录后,在CMD命令行中执行以下命令:

cmd
1
java -jar demo-spring-boot-1.0.0-SNAPSHOT.jar

启动后会在窗口中输出日志,并且可以通过http://localhost:8080/demo访问。

2 开发工具

使用开发工具可以实现代码热部署,无需重新启动应用。

导入依赖:

pom.xml
1
2
3
4
5
6
<!-- Spring Boot Developer Tools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

评论