天天看點

Spring Boot學習--spring-boot-starter-parent及starters

在官方文檔的第三部分的13塊講述了引用的管理,官方推薦的是使用Maven和Gradle。

我一直在用的是maven,而且使用maven有些優勢–spring-boot-starter-parent,這個部件是maven獨有的。

這次我們從這裡開始學習。

Maven的使用者可以通過繼承spring-boot-starter-parent項目來獲得一些合理的預設配置。這個parent提供了以下特性:

  • 預設使用Java 8
  • 使用UTF-8編碼
  • 一個引用管理的功能,在dependencies裡的部配置設定置可以不用填寫version資訊,這些version資訊會從spring-boot-dependencies裡得到繼承。
  • 識别過來資源過濾(Sensible resource filtering.)
  • 識别插件的配置(Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).)
  • 能夠識别application.properties和application.yml類型的檔案,同時也能支援profile-specific類型的檔案(如: application-foo.properties and application-foo.yml,這個功能可以更好的配置不同生産環境下的配置檔案)。
  • maven把預設的占位符${…​}改為了@..@(這點大家還是看下原文自己了解下吧,我個人用的也比較少

    since the default config files accept Spring style placeholders (${…​}) the Maven filtering is changed to use @..@ placeholders (you can override that with a Maven property resource.delimiter).)

spring-boot-starter-parent的引用

<!-- Inherit defaults from Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
           

如果dependencies中的一些引用不想使用預設的版本,可以直接加上version資訊,把預設的覆寫掉。

另外官方提供的覆寫預設配置的方式如下:

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

在properties中注明某個引用要使用的版本。具體使用哪種方式還是看個人習慣。

如果不想使用spring-boot-starter-parent,也可以自己來配置所要使用的版本:

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.0.BUILD-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
           

但是,這種方式下如果想要某些引用的版本特殊說明,就要在上面的聲明之前配置:

<dependencyManagement>
    <dependencies>
        <!-- Override Spring Data release train provided by Spring Boot -->
        <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.BUILD-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
           
這裡的引用聲明是借助scope=import 來實作的。

如果想要把項目打包成一個可執行的jar包,需要添加maven的一下元件:

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

這裡前邊文章中都有說過,位置一般都是放在dependencies之後。

繼續閱讀