1. 程式人生 > >SpringBoot 2.X課程學習 | 第二篇:讓依賴管理更加便捷(Dependency Management)

SpringBoot 2.X課程學習 | 第二篇:讓依賴管理更加便捷(Dependency Management)

一、前言 

      傳統我們搭建SSM專案的時候,使用maven做jar依賴管理的時候,還需要我們配置依賴jar包相應的版本,並且構建專案的時候,是需要什麼jar包就匯入什麼jar包,並未對jar進行系統的歸結和管理。而springboot改變了這一現狀,他相當於對maven依賴上進行更為全面的歸結和管理。

      官網中也說明了springboot依賴管理:

      Each release of Spring Boot provides a curated list of dependencies that it supports. In practice, you do not need to provide a version for any of these dependencies in your build configuration, as Spring Boot manages that for you. When you upgrade Spring Boot itself, these dependencies are upgraded as well in a consistent way,The curated list contains all the spring modules that you can use with Spring Boot as well as a refined list of third party libraries.

      上述解釋是:每一個SpringBoot版本都提供了一個它所支援的依賴關係的管理列表。實際上,您不需要在構建配置中為這些依賴項中的任何一個提供版本,因為Spring引導會為您管理這個版本。當您升級Spring引導本身時,這些依賴項也會以一致的方式升級,管理列表包含所有可以與Spring引導一起使用的Spring模組,以及第三方庫的優化列表。

二、springboot如何實現依賴管理的(maven方式)?

     當用maven來作專案依賴管理的時候,我們可以在pom.xml檔案中看到如下配置:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

       它引入spring-boot-starter-parent父專案,咱們可以繼續看spring-boot-starter-parent專案有哪些依賴,我們可以在idea工具中,使用ctrl可以直接進入到專案依賴pom檔案中(點選spring-boot-starter-parent):

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>

       它是繼續依賴spring-boot-dependencies這個專案,同樣的方式進入到spring-boot-dependencies專案POM檔案中,主要看properties屬性和dependencyManagement屬性:

<properties>
<activemq.version>5.15.9</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.73</appengine-sdk.version>
<artemis.version>2.6.4</artemis.version>
<aspectj.version>1.9.2</aspectj.version>
<assertj.version>3.11.1</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<bitronix.version>2.1.4</bitronix.version>



<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>

因為內容較多我只列舉其中的一部分內容

      從POM檔案中可以看到spring-boot-dependencies專案的POM檔案才是真正管理springboot專案依賴的。他相當於jar包的依賴仲裁,他提供了jar包的版本管理,以及spring框架和其他第三方元件jar包的依賴管理。

     使用這種依賴管理包括兩種方式:

     1.(預設方式)繼承Spring Boot的提供的父工程,需要在pom.xml中配置,如下:   

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

      2. 通過scope=import的方式引入, 在很多時候我們需要繼承自有的父工程或由於其他設定無法使用Spring Boot提供的父工程。此時可以通過scope=import的方式進行引入,如下:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.4.BUILD-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

      此處通過scope=import的方式匯入了依賴的管理配置。但此時我們無法通過在properties中覆蓋對應的屬性來完成version的控制(因為沒有繼承父工程)。以此應對的方式是通過在dependencyManagement中進行配置,並且要求在spring-boot-dependencies之前新增即可。同時,對應spring-boot-maven-plugin外掛也需要顯式配置才可以。

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

三、springboot的場景啟動器——Starters

       Starters是一組方便的依賴性描述符,可以包括在應用程式中。您可以為所有Spring和您需要的相關技術元件提供一站式服務,比如“spring-boot-starter-web”,它幫我們匯入web模組正常執行所依賴的元件jar包。springboot提供了多種場景啟動器,我們可以參照文件檢視各種啟動器如何匯入和使用,以及相關的jar依賴,相關連結。所有的啟動器都遵循類似的命名模式;SpringBootStarter-*,其中*是特定型別的應用程式。