spring-boot-03 Spring Boot的依賴是如何管理的?如何迅速創建一個Spring Boot項目?(pom.xml詳解,spring boot starter詳解)

轉載請注明來源 賴賴的博客

導語

深入一點,迷惑就少一點。

本節接著spring-boot-02,主要目的是為了了解項目的依賴方面,具體的說也就是pom.xml(我使用 maven進行依賴管理)
如果你不熟悉maven建議可以先去熟悉一下,否則以下內容可能會比較難懂,當然也可以一邊看一邊查找相關內容,并不是很復雜。

實例

項目工程目錄結構和代碼獲取地址

獲取地址(TAG將會注明不同版本對應的課程)

https://github.com/laiyijie/Spring-Boot-Learning

本節示例請參考 spring-boot-02

項目詳解

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>me.laiyijie</groupId>
    <artifactId>spring-boot-learning</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

核心點是兩部分,一個是<parent>標簽,一個是<dependency>標簽

我們不妨從 spring-boot-starter-parent 入手,看看它到底是啥。

parent

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
</parent>
spring-boot-starter-parent-1.5.4.RELEASE.pom
<?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-dependencies</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath>../../spring-boot-dependencies</relativePath>
    </parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <packaging>pom</packaging>
    ......
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
                <exclusions>
                    ......
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <!-- Turn on filtering by default for application properties -->
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/application*.yml</include>
                    <include>**/application*.yaml</include>
                    <include>**/application*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>**/application*.yml</exclude>
                    <exclude>**/application*.yaml</exclude>
                    <exclude>**/application*.properties</exclude>
                </excludes>
            </resource>
        </resources>
        <pluginManagement>
            ......
        </pluginManagement>
    </build>
</project>

我使用......來代替一些現在不關注的內容,我們可以看到 spring-boot-starter-parent-1.5.4.RELEASE.pom 做了這么四件事情:

  1. 繼承了spring-boot-dependencies(我們等下再來看這是個什么東西)
  2. 增加了一個依賴管理(dependencyManagement),只管理了spring-core的版本
  3. 配置資源的解析(resource),具體作用可以參考這里
  4. 配置了maven插件管理(pluginManagement)這里暫不贅述

那么spring-boot-dependencies是個啥呢?

spring-boot-dependencies-1.5.4.RELEASE.pom
<?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>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>1.5.4.RELEASE</version>
    <packaging>pom</packaging>
    ......
    <properties>
        <!-- Dependency versions -->
        <activemq.version>5.14.5</activemq.version>
        ......
        <versions-maven-plugin.version>2.2</versions-maven-plugin.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <version>1.5.4.RELEASE</version>
            </dependency>
            ......
        </dependencies>
    </dependencyManagement>
    <build>
        <pluginManagement>
            ......
        </pluginManagement>
        <plugins>
            ......
        </plugins>
    </build>
</project>

做了如下幾件事:

  1. 增加依賴管理(dependencyManagement)
  2. 增加maven插件管理(pluginManagement)
  3. 增加maven插件(plugins)

這樣的話,我們可以綜合起來看,
spring-boot-starter-parent-1.5.4.RELEASE.pom 做了這么3件事情

  1. 增加了一個依賴管理(dependencyManagement),管理了常用的版本
  2. 配置資源的解析(resource),具體作用可以參考這里
  3. 配置了maven插件管理(pluginManagement)以及增加了一些插件(這里暫不贅述)

也就是在<parent>這個標簽中,我們沒有引入任何依賴!只引入了一些依賴管理

明白了<parent>所做的事情,也就很容易理解為什么spring官方文檔會墻裂推薦這種引入方式,因為很簡單的就完成了上述的三件事情,否則要通過比較復雜的方式實現這種功能(分別在 依賴管理,插件管理,插件引入這個pom)。

dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web.pom
<?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-starters</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <artifactId>spring-boot-starter-web</artifactId>
    ......
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        ......
    </dependencies>
</project>

你可以認為只做了一件事:

  1. 增加了創建一個web程序所需要的依賴,而且這些依賴的版本早已經在parent繼承的時候就被確認了,所以我們在引入這個依賴的時候并不需要寫版本號!

但是目前看起來還是很復雜,因為我們對spring-boot-starter-web、spring-boot-dependenciesspring-boot-starter-parent之間的關系并不清晰!所以我畫了一張圖

spring-boot常用pom之間的依賴關系圖

spring-boot-starter依賴關系圖

以下討論暫時忽略對maven插件的管理(plugin和pluginMangement)

  1. spring-boot-dependencies 是依賴管理(dependencyManagement)
  2. spring-boot-starter-parent 增加了必要的資源處理(對application.properties和application.yaml進行了轉義)
  3. spring-boot-starters 集合了所有的spring-boot-starter-xxx
  4. spring-boot-starter-xxx 真正定義了所有的依賴

你還在害怕繼承 spring-boot-starter-parent會造成什么不必要的影響嗎?不妨看看源碼吧!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容