SpringBoot2.x入門:快速建立一個SpringBoot應用

throwable發表於2020-07-01

前提

這篇文章是《SpringBoot2.x入門》專輯的第2篇文章,使用的SpringBoot版本為2.3.1.RELEASEJDK版本為1.8

常規的套路會建議使用Spring官方提供的工具Spring Initializr通過指定配置建立一個SpringBoot專案,但是考慮到Spring Initializr必須聯網使用,對於專案配置和依賴的控制粒度不夠精細,本文會從更一般的情況考慮,詳細分析怎麼通過MavenIntelliJ IDEA(下稱IDEA)快速建立一個SpringBoot應用,包括單模組的Maven和多模組的Maven應用建立。

依賴分析

必要的外掛:

  • Maven編譯外掛:maven-compiler-plugin
  • SpringBoot封裝的Maven外掛(一般必選,專案最終打包依賴到這個外掛,它的版本建議跟隨選用的SpringBoot的版本):spring-boot-maven-plugin

Maven編譯外掛儘可能選用高版本,以適配更高版本的JDK。一般會選用的基本依賴如下:

  • lombok(可選,個人認為能提高開發效率,不過需要安裝對應的外掛)。
  • junitspring-boot-starter-test):單元測試。
  • spring-boot-starterBean管理、配置讀取等,簡單理解就是IOC容器核心元件和一些擴充套件。
  • spring-boot-starter-web:基於spring-boot-starter擴充套件,主要整合了SpringMVC的功能。

多數情況下,選用spring-boot-starter-web即可,版本選取REALEASE版本即可,注意儘可能整套專案使用同一個大版本的SpringBoot。

下面例子用到的各個元件的版本如下:

序號 元件 版本號 描述
1 maven-compiler-plugin 3.8.1 Maven編譯外掛
2 spring-boot-starter 2.3.1.RELEASE IOC容器核心元件
3 spring-boot-maven-plugin 2.3.1.RELEASE SpringBoot封裝的Maven外掛
4 lombok 1.18.12 -

建立一個單模組的SpringBoot應用

點選IDEA主選單File -> Project進入建立新專案的介面:

選擇左側的Maven選項,上方下拉選擇好JDK版本,勾選Create from archetype,然後選中maven-archetype-webapp這個骨架的RELEASE版本,然後點選下一步按鈕:

輸入專案的GAV,選定專案的磁碟目錄,然後點選下一步按鈕:

選定Maven的安裝路徑、配置檔案和本地倉庫,配置好相應的屬性,最後點選完成即可:

建立出來的是一個標準的Maven專案,它的結構如下:

spring-boot-guide
   - src
     - main
       - webapp
         - web.xml
   - pom.xml

一般可以直接刪除src/main/webapp目錄,在pom.xml中增加對應的依賴,最終的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>club.throwable</groupId>
    <artifactId>spring-boot-guide</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 指定打包方式為Jar -->
    <packaging>jar</packaging>
    <name>spring-boot-guide</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
        <lombok.version>1.18.12</lombok.version>
        <spring.boot.version>2.3.1.RELEASE</spring.boot.version>
    </properties>
    <!-- BOM全域性管理starter版本 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <!-- 指定最終打出來的Jar包的名稱 -->
        <finalName>spring-boot-guide</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
<!--                <configuration>-->
<!--                    <mainClass>可選配置,這裡填寫啟動類的全類名</mainClass>-->
<!--                </configuration>-->
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

有依賴版本變動,只需要直接修改properties元素中對應的版本號即可。在src/main下新建啟動類java/club/throwable/App.java

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@Slf4j
@SpringBootApplication
public class App implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("Hello SpringBoot!");
    }
}

啟動App中的main函式後輸出如下:

spring-boot-starter模組引入的只是核心容器元件,並沒有整合像Tomcat這樣的Servlet容器,啟動後不會掛起主執行緒,所以執行完CommandLineRunner中的邏輯就會自行退出主執行緒。

建立一個多模組的SpringBoot應用

多模組應用的建立基於單模組應用,準確來說就是在一個建立完的單模組應用中新增新的模組(New Module)。在原來的根專案spring-boot-guide右鍵彈出選單中選擇新建模組:

後續的步驟與上一小節的過程完全相同,不過定義的模組名稱必須和根專案的名稱不相同,這裡定義為ch0-dependency,然後調整父pom.xml和子pom.xml

  • spring-boot-guide -> 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>club.throwable</groupId>
    <artifactId>spring-boot-guide</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>ch0-dependency</module>
    </modules>
    <packaging>pom</packaging>
    <name>spring-boot-guide</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
        <lombok.version>1.18.12</lombok.version>
        <spring.boot.version>2.3.1.RELEASE</spring.boot.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>spring-boot-guide</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • spring-boot-guide/ch0-dependency -> 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>
    <parent>
        <groupId>club.throwable</groupId>
        <artifactId>spring-boot-guide</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>ch0-dependency</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>ch0-dependency</name>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>ch0-dependency</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
<!--                <configuration>-->
<!--                    <mainClass>club.throwable.App</mainClass>-->
<!--                </configuration>-->                           
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

注意:

  • spring-boot-maven-plugin一般情況下只需配置在需要打包的模組中,一般父模組是全域性管理的模組,不需要全域性定義此外掛。
  • maven-compiler-plugin可以配置在父模組中,讓所有子模組都應用此外掛。
  • spring-boot-starter-testlombok可以在父模組的dependencies元素中新增,相當於所有子模組都引入了這兩個依賴。

父模組中的spring-boot-guidesrc模組需要丟棄,可以直接剪下到ch0-dependency子模組中,如下:

後面再新增其他新的模組,直接重複上述的步驟即可。

程式碼倉庫

這裡給出本文搭建的一個多模組的SpringBoot應用的倉庫地址(持續更新):

(本文完 c-2-d e-a-20200701 8:39 AM)

相關文章