![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL1EDM5IzNzUjMx0yM1IjN0IDN3EjMwgDMyIDMy0SN1kjNykjMvwFOwIjMwIzLcVTN5YjM5IzLcd2bsJ2Lc12bj5ycn9Gbi52YuIjMwIzZtl2Lc9CX6MHc0RHaiojIsJye.png)
SpringBoot 2.7.2 學習系列,本節内容快速體驗Spring Boot,帶大家了解它的基本使用、運作和打包。
SpringBoot 2.7.2 學習系列,本節内容快速體驗Spring Boot,帶大家了解它的基本使用、運作和打包。
Spring Boot 基于 Spring 架構,底層離不開 IoC、AoP 等核心思想。Spring 4.0 提供了基于 Java Config 的開發方式,Spring Boot 應運而生,可以簡化 Spring 應用開發過程,同時也可以快速友善的內建第三方架構,如 MyBatis、Redis 等。
0 版本說明
- 開發工具 IDEA 版本:2021.2
- Maven 版本: 3.6.3
- Spring Boot 版本:2.7.2
- JDK 版本:JDK 8
- MySQL 版本:MySQL 8
說明,目前 Spring Boot 2.x 最新穩定版為 2.7.2
,JDK 8 需要以上版本、Maven 需要 3.5 以上版本。(本想基于 Spring Boot 3.x,但 3.x 需要 Java 17,優雅哥電腦還隻是 JDK 8)
1 建立 Spring Boot 應用
1.1 建立工程
1)打開 idea,建立一個 Maven 項目,點選“Next”
2)輸入 Name、GroupId、ArtifactId,點選“Finish”
1.2 設定 IDEA
在 IDEA 的 Preferences 中設定 JDK、Maven
1)設定 Maven
2)設定 JDK
在 Module Settings 中設定 JDK
首先設定 SDKs:
接着在 “Project” 中選擇設定的 SDK
最後在 “Modules” 中選擇 Language Level:
2 添加 Spring Boot 支援
2.1 添加依賴
1)在
pom.xml
中添加 Spring Boot 依賴。
在爛大街的文章部落格中,都是通過
parent
的方式繼承
spring-boot-starter-parent
,如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
</parent>
這種方式幾乎都用于在 demo 編寫中,在大型項目或中大型企業中很少見到這麼使用的。因為每個 module 隻能有一個 parent ,而在企業開發中,微服務有多個服務,多個服務一般會繼承自一個統一的 module,便于版本控制、通用功能等。如果在每個服務中都讓spring-boot-starter-parent 占據了 parent 節點,那如何繼承統一的 parent module 呢?
或許有人會說,在 parent module 中繼承 spring-boot-starter-parent。沒錯,确實可以這樣。但除了服務,還會有一些公共子產品(如對參數校驗、通用響應、分布式 Redis 鎖、Spring Doc等通用子產品)也繼承自這個parent module,這樣一來,這些公共module也被迫添加了壓根沒有使用 spring boot starter 依賴。
我的做法是通過
spring-boot-dependencies
來實作:
- 在
中定義 spring-boot-dependencies 版本号properties
- 在依賴管理
中,通過 pom 的方式、scope 為 import 添加dependencyManagement
spring-boot-dependencies
- 在依賴
中添加需要使用到的依賴。dependencies
pom.xml
檔案如下:
<?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>
<groupId>com.yygnb.demo</groupId>
<artifactId>hero-springboot-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<!-- Spring Boot 版本 -->
<spring-boot-dependencies.version>2.7.2</spring-boot-dependencies.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.2 建立啟動類
建立啟動類:com.yygnb.demo.DemoApplication
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
3 測試運作
3.1 添加測試Controller
建立測試使用的 Controller:com.yygnb.demo.controller.DemoController
@RestController
@RequestMapping("demo")
public class DemoController {
@GetMapping("hello")
public String hello(String msg) {
String result = "Hello Spring Boot ! " + msg;
System.out.println(result);
return result;
}
}
3.2 啟動運作
運作
DemoApplication
中的
main
方法即可啟動應用。控制台顯示如下,則應用啟動成功:
在浏覽器中通路:
http://localhost:8080/demo/hello?msg=FirstTest
頁面上和控制台中都會顯示:
Hello Spring Boot ! FirstTest
3.3 修改端口
Spring Boot 預設運作的端口為 8080,如果要修改運作的端口号,需要修改配置實作。Spring Boot 支援三種格式的配置檔案:
xml
、
yml
、
yaml
,在項目中使用
yml
格式較多。
使用 yml 格式需要注意:
1. 使用冒号分隔屬性名和屬性值
2. 字元大小寫敏感
3. 層級之間縮進敏感(間隔兩個空格)
4. 屬性值前面需要加一個空格
Spring Boot 核心配置檔案名為
application
。
在
src/main/resources
中建立配置檔案
application.yml
server:
port: 9099
該配置指定了服務運作的端口号為 9099,重新開機服務,将上面的通路路徑中的8080修改為 9099 再次通路。
4 打包
Maven 工程打包是通過
mvn
指令進行的。在控制台中項目根目錄下執行下列指令:
mvn clean package
執行該指令後,會在
target
目錄下生成
hero-springboot-demo-1.0-SNAPSHOT.jar
。檢視該檔案的大小,隻有 4kb,這是因為直接打包僅僅會打包目前的代碼,相關使用到的 spring boot 的依賴并不會包含進去,此時的 jar 檔案是不能直接運作的。如果要讓打包後的 jar 可以直接運作,需要在
pom.xml
中配置相關插件配置:
4.1 配置插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-dependencies.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
重新執行
mvn clean pacakge
,會在 target 中生成兩個檔案:
- hero-springboot-demo-1.0-SNAPSHOT.jar.original
- hero-springboot-demo-1.0-SNAPSHOT.jar
前者是打包的原始檔案,和配置插件前一樣,僅包含目前項目的代碼。後者就包含了 spring boot 有關依賴,内置 Tomcat,可以直接運作。
4.2 運作 jar
可通過 java 指令執行上面打包生成的可執行檔案。在控制台中執行:
java -jar target/hero-springboot-demo-1.0-SNAPSHOT.jar
指令執行後,依舊會啟動服務。
本文記錄了另一種依賴 spring boot 的方式,下一篇內建MyBatis Plus實作增删改查。
今日優雅哥(youyacoder)學習結束,期待關注留言分享~~