部署之前 环境:Ubuntu 20.04
过程参考 Spring 官方文档
Spring Boot Reference Documentation
安装依赖 Spring Boot 2.7.0 requires Java 8 and is compatible up to and including Java 18. Spring Framework 5.3.20 or above is also required.
Explicit build support is provided for the following build tools:
Build Tool
Version
Maven
3.5+
Gradle
6.8.x, 6.9.x, and 7.x
JDK 选用了Liberica JDK 11
Liberica Package Repositories | BellSoft Java (bell-sw.com)
首先把它的软件源加进来
1 2 wget -q -O - https://download.bell-sw.com/pki/GPG-KEY-bellsoft | sudo apt-key add - echo "deb [arch=amd64] https://apt.bell-sw.com/ stable main" | sudo tee /etc/apt/sources.list.d/bellsoft.list
然后更新源,安装
1 2 sudo apt-get update sudo apt-get install bellsoft-java11
修改环境变量
1 2 3 export JAVA_HOME=/usr/lib/jvm/bellsoft-java11-amd64/export PATH=$PATH :$JAVA_HOME /bin
Maven
Spring Boot is compatible with Apache Maven 3.3 or above
参考 Maven 官网的引导进行
通过 wget 下载
1 wget https://dlcdn.apache.org/maven/maven-3/3.8.5/binaries/apache-maven-3.8.5-bin.tar.gz
解压一下
1 tar xzvf apache-maven-3.8.5-bin.tar.gz
添加环境变量
1 2 export PATH=$PATH :~/apache-maven-3.8.5/bin
检查是否配置正确
1 2 3 4 5 6 bakaft@BakaFT-PC:~/apache-maven-3.8.5/bin$ mvn -v Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0) Maven home: /home/bakaft/apache-maven-3.8.5 Java version: 11.0.15, vendor: BellSoft, runtime: /usr/lib/jvm/bellsoft-java11-amd64 Default locale: en, platform encoding: UTF-8 OS name: "linux" , version: "5.10.16.3-microsoft-standard-wsl2" , arch : "amd64" , family: "unix"
(可选)Spring-CLI 官网的文档推荐使用Spring-CLI
进行管理
下载 1 2 wget https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.7.0/spring-boot-cli-2.7.0-bin.tar.gz tar xzvf spring-boot-cli-2.7.0-bin.tar.gz
设置环境变量 1 2 3 export SPRING_HOME=~/spring-2.7.0export PATH=$PATH :$SPRING_HOME /bin
运行一个最小 Spring Boot 实例 新建一个文件app.groovy
,写入:
1 2 3 4 5 6 7 8 9 @RestController class ThisWillActuallyRun { @RequestMapping ("/" ) String home() { "Hello World!" } }
使用 Spring-CLI 运行它
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 bakaft@BakaFT-PC:~/java-projects$ spring run app.groovy Resolving dependencies................................. . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | ' _ | '_| | ' _ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.0) 2022-05-24 18:56:03.544 INFO 194 --- [ runner-0] o.s.boot.SpringApplication : Starting application using Java 11.0.15 on BakaFT-PC with PID 194 (started by bakaft in /home/bakaft/java-projects) 2022-05-24 18:56:03.549 INFO 194 --- [ runner-0] o.s.boot.SpringApplication : No active profile set, falling back to 1 default profile: "default" 2022-05-24 18:56:04.344 INFO 194 --- [ runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-05-24 18:56:04.356 INFO 194 --- [ runner-0] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-05-24 18:56:04.356 INFO 194 --- [ runner-0] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.63] 2022-05-24 18:56:04.379 INFO 194 --- [ runner-0] org.apache.catalina.loader.WebappLoader : Unknown class loader [org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@7a3d45bd] of class [class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader] 2022-05-24 18:56:04.407 INFO 194 --- [ runner-0] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-05-24 18:56:04.407 INFO 194 --- [ runner-0] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 691 ms 2022-05-24 18:56:04.543 WARN 194 --- [ runner-0] o.s.b.a.AutoConfigurationPackages : @EnableAutoConfiguration was declared on a class in the default package. Automatic @Repository and @Entity scanning is not enabled. 2022-05-24 18:56:04.903 INFO 194 --- [ runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ' ' 2022-05-24 18:56:04.913 INFO 194 --- [ runner-0] o.s.boot.SpringApplication : Started application in 1.769 seconds (JVM running for 46.504) 2022-05-24 18:58:42.253 INFO 194 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet ' dispatcherServlet' 2022-05-24 18:58:42.254 INFO 194 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet ' dispatcherServlet' 2022-05-24 18:58:42.255 INFO 194 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
可以看到,这样就启动了一个非常小的 Spring Boot 实例
1 2 bakaft@BakaFT-PC:~$ curl http://localhost:8080 -w '\n' Hello World!
创建一个 Spring Boot 项目 Spring Boot 项目本质上还是一个 Maven(或者 Gradle)项目
编写 pom.xml 创建一个新的文件夹,让 Maven 可以来初始化项目
在这个文件夹里创建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 <?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 > <groupId > me.bakaft</groupId > <artifactId > spring-boot-demo</artifactId > <version > 0.0.1-SNAPSHOT</version > <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.7.0</version > </parent > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > </dependencies > </project >
这个时候,你就可以将你的这个 Maven 项目导入 IDE 进行开发了,不过接下来继续使用命令行进行
接下来更新 Maven 的依赖树,第一次运行会先下载依赖
在运行一次就可以看到当前项目的依赖树了
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 39 40 41 42 43 44 45 46 bakaft@BakaFT-PC:~/java-projects/spring-boot-demo$ mvn dependency:tree [INFO] Scanning for projects... [INFO] [INFO] ---------------------< me.bakaft:spring-boot-demo >--------------------- [INFO] Building spring-boot-demo 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-dependency-plugin:3.3.0:tree (default-cli) @ spring-boot-demo --- [INFO] me.bakaft:spring-boot-demo:jar:0.0.1-SNAPSHOT [INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.7.0:compile [INFO] +- org.springframework.boot:spring-boot-starter:jar:2.7.0:compile [INFO] | +- org.springframework.boot:spring-boot:jar:2.7.0:compile [INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.7.0:compile [INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.7.0:compile [INFO] | | +- ch.qos.logback:logback-classic:jar:1.2.11:compile [INFO] | | | +- ch.qos.logback:logback-core:jar:1.2.11:compile [INFO] | | | \- org.slf4j:slf4j-api:jar:1.7.36:compile [INFO] | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.17.2:compile [INFO] | | | \- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile [INFO] | | \- org.slf4j:jul-to-slf4j:jar:1.7.36:compile [INFO] | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile [INFO] | +- org.springframework:spring-core:jar:5.3.20:compile [INFO] | | \- org.springframework:spring-jcl:jar:5.3.20:compile [INFO] | \- org.yaml:snakeyaml:jar:1.30:compile [INFO] +- org.springframework.boot:spring-boot-starter-json:jar:2.7.0:compile [INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.13.3:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.3:compile [INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.13.3:compile [INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.13.3:compile [INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.13.3:compile [INFO] | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.13.3:compile [INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.7.0:compile [INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.63:compile [INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.63:compile [INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.63:compile [INFO] +- org.springframework:spring-web:jar:5.3.20:compile [INFO] | \- org.springframework:spring-beans:jar:5.3.20:compile [INFO] \- org.springframework:spring-webmvc:jar:5.3.20:compile [INFO] +- org.springframework:spring-aop:jar:5.3.20:compile [INFO] +- org.springframework:spring-context:jar:5.3.20:compile [INFO] \- org.springframework:spring-expression:jar:5.3.20:compile [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.939 s [INFO] Finished at: 2022-05-24T19:20:04+08:00
编写 RestController Maven 默认从src/main/java
编译源码
1 2 3 bakaft@BakaFT-PC:~/java-projects/spring-boot-demo$ mkdir -p src/main/java bakaft@BakaFT-PC:~/java-projects/spring-boot-demo$ cd src/main/java/ bakaft@BakaFT-PC:~/java-projects/spring-boot-demo/src/main/java$ vi MyApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController @EnableAutoConfiguration public class MyApplication { @RequestMapping("/") String home () { return "Hi!This is my first Spring Boot application from scratch" ; } public static void main (String[] args) { SpringApplication.run(MyApplication.class, args); } }
运行测试 第一次运行会下载一些东西,多等等
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 39 40 41 42 43 44 45 46 47 48 49 50 bakaft@BakaFT-PC:~/java-projects/spring-boot-demo$ mvn spring-boot:run [INFO] Scanning for projects... [INFO] [INFO] ---------------------< me.bakaft:spring-boot-demo >--------------------- [INFO] Building spring-boot-demo 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] >>> spring-boot-maven-plugin:2.7.0:run (default-cli) > test-compile @ spring-boot-demo >>> [INFO] [INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ spring-boot-demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Using 'UTF-8' encoding to copy filtered properties files. [INFO] skip non existing resourceDirectory /home/bakaft/java-projects/spring-boot-demo/src/main/resources [INFO] skip non existing resourceDirectory /home/bakaft/java-projects/spring-boot-demo/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ spring-boot-demo --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ spring-boot-demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Using 'UTF-8' encoding to copy filtered properties files. [INFO] skip non existing resourceDirectory /home/bakaft/java-projects/spring-boot-demo/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ spring-boot-demo --- [INFO] No sources to compile [INFO] [INFO] <<< spring-boot-maven-plugin:2.7.0:run (default-cli) < test-compile @ spring -boot-demo <<< [INFO] [INFO] [INFO] --- spring-boot-maven-plugin:2.7.0:run (default-cli) @ spring-boot-demo --- [INFO] Attaching agents: [] . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | ' _ | '_| | ' _ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.0) 2022-05-24 19:26:10.087 INFO 900 --- [ main] MyApplication : Starting MyApplication using Java 11.0.15 on BakaFT-PC with PID 900 (/home/bakaft/java-projects/spring-boot-demo/target/classes started by bakaft in /home/bakaft/java-projects/spring-boot-demo) 2022-05-24 19:26:10.090 INFO 900 --- [ main] MyApplication : No active profile set, falling back to 1 default profile: "default" 2022-05-24 19:26:10.547 INFO 900 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-05-24 19:26:10.554 INFO 900 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-05-24 19:26:10.554 INFO 900 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.63] 2022-05-24 19:26:10.612 INFO 900 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-05-24 19:26:10.612 INFO 900 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 496 ms 2022-05-24 19:26:10.698 WARN 900 --- [ main] o.s.b.a.AutoConfigurationPackages : @EnableAutoConfiguration was declared on a class in the default package. Automatic @Repository and @Entity scanning is not enabled. 2022-05-24 19:26:10.819 INFO 900 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ' ' 2022-05-24 19:26:10.826 INFO 900 --- [ main] MyApplication : Started MyApplication in 0.995 seconds (JVM running for 1.205)
看一下效果
1 2 bakaft@BakaFT-PC:~$ curl http://localhost:8080 -w '\n' Hi!This is my first Spring Boot application from scratch