測試環境 maven 3.3.9web
想必你們在作SpringBoot應用的時候,都會有以下代碼:spring
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
繼承一個父模塊,而後再引入相應的依賴maven
假如說,我不想繼承,或者我想繼承多個,怎麼作?spring-boot
咱們知道Maven的繼承和Java的繼承同樣,是沒法實現多重繼承的,若是10個、20個甚至更多模塊繼承自同一個模塊,那麼按照咱們以前的作法,這個父模塊的dependencyManagement會包含大量的依賴。若是你想把這些依賴分類以更清晰的管理,那就不可能了,import scope依賴能解決這個問題。你能夠把dependencyManagement放到單獨的專門用來管理依賴的pom中,而後在須要使用依賴的模塊中經過import scope依賴,就能夠引入dependencyManagement。例如能夠寫這樣一個用於依賴管理的pom:測試
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.sample</groupId>
<artifactId>base-parent1</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactid>junit</artifactId>
<version>4.8.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactid>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
而後我就能夠經過非繼承的方式來引入這段依賴管理配置.net
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.test.sample</groupId>
<artifactid>base-parent1</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>junit</groupId>
<artifactid>junit</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactid>log4j</artifactId>
</dependency>設計
注意:import scope只能用在dependencyManagement裏面對象
這樣,父模塊的pom就會很是乾淨,由專門的packaging爲pom來管理依賴,也契合的面向對象設計中的單一職責原則。此外,咱們還可以建立多個這樣的依賴管理pom,以更細化的方式管理依賴。這種作法與面向對象設計中使用組合而非繼承也有點類似的味道。blog
那麼,如何用這個方法來解決SpringBoot的那個繼承問題呢?繼承
配置以下:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
這樣配置的話,本身的項目裏面就不須要繼承SpringBoot的module了,而能夠繼承本身項目的module了。轉自:https://blog.csdn.net/mn960mn/article/details/50894022