使用 spring-boot-dependencies(spring-boot-starter-parent) 方便管理项目依赖

当我们使用 spring 或 spring-boot 开发项目时,需要引入很多依赖,包括 spring 本身的组件、各种 spring-boot-starter、以及其它第三方依赖(如:slf4j、redis)。
依赖多了,版本的选择是个问题,就怕哪个版本选择的不对导致出现一些意想不到的 BUG。
比如下面这个例子:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.26</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.14</version>
</dependency>
</dependencies>
slf4j 的 1.7.26 版本、postgresql 的 42.2.14 版本和 spring 的 5.2.10.RELEASE 版本会不会有冲突呢???
于是 spring 提供了 第一种是继承spring-boot-starter-parent [推荐,常用]第二种是通过dependencyManagement进行依赖管理的spring-boot-dependencies ,只需在 dependencyManagement 里面配置好 spring-boot-dependencies 的版本,在 dependencies 里面就不用再指定其它版本号了。
这两种方式,我们在使用spring-boot-starter的时候都不需要指定版本:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
</dependencies>
其实 spring-boot-dependencies 就是一个 pom.xml,里面管理了很多常见第三方的依赖和版本。
继承spring-boot-starter-parent其实也算是继承自spring-boot-dependencies,我们点开spring-boot-starter-parent,可以看到parent其实也是继承dependencies,parent里面就增加了一些插件,然后指定了maven编译版本
有些人喜欢用spring-boot-starter-parent这样的配置:
经查阅官方文档得知,可能有人不喜欢继承spring-boot-starter-parent POM。也可能有自己的企业标准parent。如果你不想使用spring-boot-starter-parent,你依然可以通过使用spring-boot-dependencies的scope=import利用依赖管理的便利。




更多推荐
所有评论(0)