使用Spring Boot的兩種方式

繼承spring-boot-starter-parent

如果繼承spring-boot-starter-parent,需在pom.xml中加入如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.M3</version>
</parent>

注:在此需要指定Spring Boot的版本號,如果要導入其他的starters,則可省略版本號。

在這種設置情況下,你也可以通過在自己的Pom覆蓋一屬性值的方式覆蓋個別依賴。如,要升級spring data發佈版:

<properties>
    <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>

不繼承Spring Boot paren POM

並不是每個人都喜歡繼承spring-boot-starter-parent,可能是因爲有自己需要使用的企業父項目,或者你僅僅喜歡明確的聲明你的所有MAVEN配置。

如果不想繼承spring-boot-starter-parent,但想要使用其依賴管理可以以如下的方式使用:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.0.M3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

使用此種方式就能像上面闡述的那樣可以在自己項目Pom中改變一個屬性的性即可覆蓋個別依賴。爲了達到這種效果,你需要在spring-boot-dependencies之前添加對應的依賴:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Fowler-SR2</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.0.M3</version>
            <type>pom</type>
            <scope>import</scope>
        </denpendency>
    </dependencies>
</dependencyManagement>
發佈了42 篇原創文章 · 獲贊 10 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章