Chinaunix首页 | 论坛 | 博客
  • 博客访问: 726287
  • 博文数量: 225
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2722
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-03 17:32
文章分类

全部博文(225)

文章存档

2019年(7)

2018年(16)

2017年(1)

2016年(26)

2015年(41)

2014年(15)

2013年(119)

我的朋友

分类: Java

2018-06-01 11:59:33

测试环境 maven 3.3.9

想必大家在做SpringBoot应用的时候,都会有如下代码:


  1. <parent>  
  2.     <groupId>org.springframework.bootgroupId>  
  3.     <artifactId>spring-boot-starter-parentartifactId>  
  4.     <version>1.3.3.RELEASEversion>  
  5. parent>  
继承一个父模块,然后再引入相应的依赖


假如说,我不想继承,或者我想继承多个,怎么做?


我们知道Maven的继承和Java的继承一样,是无法实现多重继承的,如果10个、20个甚至更多模块继承自同一个模块,那么按照我们之前的做法,这个父模块的dependencyManagement会包含大量的依赖。如果你想把这些依赖分类以更清晰的管理,那就不可能了,import scope依赖能解决这个问题。你可以把dependencyManagement放到单独的专门用来管理依赖的pom中,然后在需要使用依赖的模块中通过import scope依赖,就可以引入dependencyManagement。例如可以写这样一个用于依赖管理的pom:


[html] view plain copy
  1. <project>  
  2.     <modelVersion>4.0.0modelVersion>  
  3.     <groupId>com.test.samplegroupId>  
  4.     <artifactId>base-parent1artifactId>  
  5.     <packaging>pompackaging>  
  6.     <version>1.0.0-SNAPSHOTversion>  
  7.     <dependencyManagement>  
  8.         <dependencies>  
  9.             <dependency>  
  10.                 <groupId>junitgroupId>  
  11.                 <artifactid>junitartifactId>  
  12.                 <version>4.8.2version>  
  13.             dependency>  
  14.             <dependency>  
  15.                 <groupId>log4jgroupId>  
  16.                 <artifactid>log4jartifactId>  
  17.                 <version>1.2.16version>  
  18.             dependency>  
  19.         dependencies>  
  20.     dependencyManagement>  
  21. project>  
然后我就可以通过非继承的方式来引入这段依赖管理配置


[html] view plain copy
  1. <dependencyManagement>  
  2.     <dependencies>  
  3.         <dependency>  
  4.             <groupId>com.test.samplegroupId>  
  5.             <artifactid>base-parent1artifactId>  
  6.             <version>1.0.0-SNAPSHOTversion>  
  7.             <type>pomtype>  
  8.             <scope>importscope>  
  9.         dependency>  
  10.     dependencies>  
  11. dependencyManagement>  
  12.   
  13. <dependency>  
  14.     <groupId>junitgroupId>  
  15.     <artifactid>junitartifactId>  
  16. dependency>  
  17. <dependency>  
  18.     <groupId>log4jgroupId>  
  19.     <artifactid>log4jartifactId>  
  20. dependency>  

注意:import scope只能用在dependencyManagement里面


这样,父模块的pom就会非常干净,由专门的packaging为pom来管理依赖,也契合的面向对象设计中的单一职责原则。此外,我们还能够创建多个这样的依赖管理pom,以更细化的方式管理依赖。这种做法与面向对象设计中使用组合而非继承也有点相似的味道。


那么,如何用这个方法来解决SpringBoot的那个继承问题呢?

配置如下:


[html] view plain copy
  1. <dependencyManagement>  
  2.     <dependencies>  
  3.         <dependency>  
  4.             <groupId>org.springframework.bootgroupId>  
  5.             <artifactId>spring-boot-dependenciesartifactId>  
  6.             <version>1.3.3.RELEASEversion>  
  7.             <type>pomtype>  
  8.             <scope>importscope>  
  9.         dependency>  
  10.     dependencies>  
  11. dependencyManagement>  
  12.   
  13. <dependencies>  
  14.     <dependency>  
  15.         <groupId>org.springframework.bootgroupId>  
  16.         <artifactId>spring-boot-starter-webartifactId>  
  17.     dependency>  
  18. dependencies>  

这样配置的话,自己的项目里面就不需要继承SpringBoot的module了,而可以继承自己项目的module了。





复制代码
阅读(982) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~