天天看點

使用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>