快速體驗Spring Boot了解使用、運行和打包 | SpringBoot 2.7.2學習系列

SpringBoot 2.7.2 學習系列,本節內容快速體驗Spring Boot,帶大家了解它的基本使用、運行和打包。

Spring Boot 基於 Spring 框架,底層離不開 IoC、AoP 等核心思想。Spring 4.0 提供了基於 Java Config 的開發方式,Spring Boot 應運而生,可以簡化 Spring 應用開發過程,同時也可以快速方便的集成第三方框架,如 MyBatis、Redis 等。

0 版本說明

  1. 開發工具 IDEA 版本:2021.2
  2. Maven 版本: 3.6.3
  3. Spring Boot 版本:2.7.2
  4. JDK 版本:JDK 8
  5. 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」

image-20220725222306684

2)輸入 Name、GroupId、ArtifactId,點擊「Finish」

image-20220725222537900

1.2 設置 IDEA

在 IDEA 的 Preferences 中設置 JDK、Maven

1)設置 Maven

image-20220725223111620

2)設置 JDK

image-20220725223300552

在 Module Settings 中設置 JDK

image-20220725223448668

首先設置 SDKs:

image-20220725223543208

接着在 「Project」 中選擇設置的 SDK

image-20220725223646265

最後在 「Modules」 中選擇 Language Level:

image-20220725223758470

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 來實現:

  1. properties 中定義 spring-boot-dependencies 版本號
  2. 在依賴管理 dependencyManagement 中,通過 pom 的方式、scope 為 import 添加 spring-boot-dependencies
  3. 在依賴dependencies中添加需要使用到的依賴。

pom.xml 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="//maven.apache.org/POM/4.0.0"
         xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //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 方法即可啟動應用。控制台顯示如下,則應用啟動成功:

image-20220726003230302

在瀏覽器中訪問:

//localhost:8080/demo/hello?msg=FirstTest

頁面上和控制台中都會顯示:

Hello Spring Boot ! FirstTest

3.3 修改端口

Spring Boot 默認運行的端口為 8080,如果要修改運行的端口號,需要修改配置實現。Spring Boot 支持三種格式的配置文件:xmlymlyaml,在項目中使用 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實現增刪改查。

image

今日優雅哥(youyacoder)學習結束,期待關注留言分享~~