天天看點

Spring Boot開啟的2種方式

Spring Boot依賴

使用Spring Boot很簡單,先添加基礎依賴包,有以下兩種方式

1. 繼承spring-boot-starter-parent項目

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

2. 導入spring-boot-dependencies項目依賴

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

Spring Boot依賴注意點

1. 屬性覆寫隻對繼承有效

This only works if your Maven project inherits (directly or indirectly) from spring-boot-dependencies. If you have added spring-boot-dependencies in your own dependencyManagement section with import you have to redefine the artifact yourself instead of overriding the property.

Spring Boot依賴包裡面的元件的版本都是和目前Spring Boot綁定的,如果要修改裡面元件的版本,隻需要添加如下屬性覆寫即可,但這種方式隻對繼承有效,導入的方式無效。

<properties>
    <slf4j.version>1.7.25<slf4j.version>
</properties>      

如果導入的方式要實作版本的更新,達到上面的效果,這樣也可以做到,把要更新的元件依賴放到Spring Boot之前。

<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>1.5.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>      

Each Spring Boot release is designed and tested against a specific set of third-party dependencies. Overriding versions may cause compatibility issues.

需要注意,要修改Spring Boot的依賴元件版本可能會造成不相容的問題。

2. 資源檔案過濾問題

使用繼承Spring Boot時,如果要使用Maven resource filter過濾資源檔案時,資源檔案裡面的占位符為了使${}和Spring Boot差別開來,此時要用@…@包起來,不然無效。另外,@…@占位符在yaml檔案編輯器中編譯報錯,是以使用繼承方式有諸多問題,坑要慢慢趟。