测试环境 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