依赖管理

在SpringBoot项目中可以看到如下父项目:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.10</version>
<relativePath/>
</parent>


  • 该项目是用来做依赖管理的。当引入其他依赖时,不需要在指定版本。

查看spring-boot-starter-parent项目,可以看到它的父项目如下:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.10</version>
</parent>


进入spring-boot-dependencies,可以看到​​<properties>​​中声明了开发中常用的jar包版本:

<properties>
<activemq.version>5.16.3</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.91</appengine-sdk.version>
<artemis.version>2.15.0</artemis.version>
<aspectj.version>1.9.7</aspectj.version>
<assertj.version>3.18.1</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<awaitility.version>4.0.3</awaitility.version>
...
</properties>


总结:SpringBoot项目的父项目中几乎声明了开发中常用的依赖版本,在引入依赖时可以不需要在指定版本号

自定义依赖版本

在当前SpringBoot项目中可以通过指定​​<properties>​​来自定义依赖为我们想要的版本。

<properties>
<mysql.version>5.1.41</mysql.version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>


starters

starters 包含许多依赖项,可以使用这些依赖项来快速启动和运行项目,对于各种开发场景可能用到的依赖,用户只需要在 Maven 中引入 starter 依赖即可。所有官方依赖项都遵循​​spring-boot-starter-*​​这种类似的命名模式,其中​​*​​是特定类型的应用程序。例如如果想开始使用 Spring 和 JPA 进行数据库访问,在项目中需要包含该依赖项:​​spring-boot-starter-data-jpa​

如果官方提供的各种starter无法满足我们的需求,我们可以创建我们自己的starter。第三方创建的starter不推荐以​​spring-boot​​开头,第三方启动器通常以项目名称开头。例如,名为​​thirdpartyproject​​的第三方启动项目通常会命名为​​thirdpartyproject-spring-boot-starter​​。