1. 程式人生 > >Spring Boot開啟的2種方式

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 withimportyou 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區別開來,此時要用@[email protected]包起來,不然無效。另外,@[email protected]佔位符在yaml檔案編輯器中編譯報錯,所以使用繼承方式有諸多問題,坑要慢慢趟。