Skip to content

SpringBoot依赖管理

1. 为何导入starter-web所有相关依赖包会导入进来

利用maven依赖传递原则,若A->B->C, A就拥有了B和C的依赖。
在开发中导入什么starter,比如starter-web, starter-web就会把自身所依赖的jar包全部自动导入进来, 如下图starter-web所有的依赖有这些: Alt text

2. 为何配置不需要指定版本号

SpringBoot项目都有一个父项目spring-boot-starter-parent, 而它的父项目是spring-boot-denpendencies,在它里面已经把所有常见的依赖jar的版本声明好了, 如下图: Alt text 可以看到上图中在<properties>中配置了相关jar包的版本号信息 Alt text<dependencyManagement>中配置了相关jar包ga信息

3. 自定义jar包版本

利用maven的就近原则,本地pom.xml会覆盖父项目,有两种修改办法:

  1. <properties>中使用spring-boot-denpendencies中相同的属性名,配置上自己的版本号
  2. 直接在<dependency>中添加<version>节点,指定版本号

4. 第三方的jar包

父项目spring-boot-denpendencies中没有管理的jar包直接自行导入即可,比如:

xml
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.16</version>
</dependency>

5. SpringBoot项目依赖关系图

Alt text