SpringBoot2.x入门:依赖管理
前提#
这篇文章是《SpringBoot2.x入门》专辑的第1篇文章,使用的SpringBoot
版本为2.3.1.RELEASE
,JDK
版本为1.8
。
主要梳理一下SpringBoot2.x
的依赖关系和依赖的版本管理,依赖版本管理是开发和管理一个SpringBoot
项目的前提。
SpringBoot
其实是通过starter
的形式,对spring-framework
进行装箱,消除了(但是兼容和保留)原来的XML
配置,目的是更加便捷地集成其他框架,打造一个完整高效的开发生态。
SpringBoot依赖关系#
因为个人不太喜欢Gradle
,所以下文都以Maven
举例。
和SpringCloud
的版本(SpringCloud
的正式版是用伦敦地铁站或者说伦敦某地名的英文名称作为版本号,例如比较常用的F
版本Finchley
就是位于伦敦北部芬奇利)管理不同,SpringBoot
的依赖组件发布版本格式是:X.Y.Z.RELEASE
。因为SpringBoot
组件一般会装箱为starter
,所以组件的依赖GAV
一般为:org.springframework.boot:spring-boot-starter-${组件名}:X.Y.Z.RELEASE
,其中X
是主版本,不同的主版本意味着可以放弃兼容性,也就是SpringBoot1.x
和SpringBoot2.x
并不保证兼容性,而组件名一般是代表一类中间件或者一类功能,如data-redis
(spring-boot-starter-data-redis
,提供Redis
访问功能)、jdbc
(spring-boot-starter-jdbc
,提供基于JDBC
驱动访问数据库功能)等等。以SpringBoot
当前最新的发布版本2.3.1.RELEASE
的org.springframework.boot:spring-boot-starter:jar:2.3.1.RELEASE
为例,用mvn dependency:tree
分析它的依赖关系如下:
这个依赖树也印证了starter
是基于Spring
项目装箱和扩展的。
SpringBoot依赖管理#
如果使用Spring Initializr创建一个SpringBoot
项目的话,那么会发现项目的POM
文件中会加入了一个parent
元素:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
其实spring-boot-starter-parent
相当于作为了当前项目的父模块,在父模块里面管理了当前指定的SpringBoot
版本2.3.1.RELEASE
所有依赖的第三方库的统一版本管理,通过spring-boot-starter-parent
上溯到最顶层的项目,会找到一个properties
元素,里面统一管理Spring
框架和所有依赖到的第三方组件的统一版本号,这样就能确保对于一个确定的SpringBoot
版本,它引入的其他starter
不再需要指定版本,同时所有的第三方依赖的版本也是固定的。如项目的POM
文件如下:
<!-- 暂时省略其他的配置属性 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
这样只需要修改parent
元素中的版本号,就能全局更变所有starter
的版本号。这种做法其实本质上是把当前项目作为spring-boot-starter-parent
的子项目,其实在一定程度上并不灵活。这里推荐使用另一种方式:通过dependencyManagement
元素全局管理SpringBoot
版本,适用于单模块或者多模块的Maven
项目。项目的(父)POM
文件如下:
<!-- spring-boot-guide 父POM -->
<properties>
<spring.boot.version>2.3.1.RELEASE</spring.boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
然后需要用到其他starter
的时候,只需要在dependencies
直接引入即可,不再需要指定版本号,版本号由dependencyManagement
中定义的版本号统一管理。
<!-- spring-boot-guide/ch0-dependency 子POM -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
SpringBoot依赖覆盖#
有些特殊的情况,可能项目中大部分的starter
使用的是相对低的版本,但是由于部分新的功能需要使用到更高版本的个别starter
,则需要强制引入该高版本的starter
。这里举一个例子,项目用到的SpringBoot
组件的版本是2.1.5.RELEASE
,使用的中间件服务Elasticsearch
的版本是7.x
,而spring-boot-starter-data-elasticsearch
支持的版本如下:
理论上可以一下子升级SpringBoot
到2.3.1.RELEASE
,其实也可以直接指定spring-boot-starter-data-elasticsearch
的版本覆盖掉全局的SpringBoot
组件版本,这里应用了Maven
的依赖调解原则:
<!-- 父POM或者全局POM -->
<properties>
<spring.boot.version>2.1.5.RELEASE</spring.boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
</dependencies>
这样就能单独提升spring-boot-starter-data-elasticsearch
的版本为2.3.1.RELEASE
,其他组件的版本依然保持为2.1.5.RELEASE
。
小结#
目前有两种常用的方式管理SpringBoot
组件的版本(两种方式二选一):
- 配置
parent
元素,通过项目继承的方式指定SpringBoot
组件的版本号,这是Spring Initializr
生成的项目中默认的配置方式。 - 配置
dependencyManagement
元素(推荐此方式),通过(父)POM
文件统一指定SpringBoot
组件的版本号。
另外,SpringBoot
的1.x
和2.x
之间有兼容性问题(最明显的一点是2.x
中删除了1.x
中大量的内建类,如果用到了这些SpringBoot
中的内建类,容易出现ClassNotFoundException
),降级或者升级都有比较大的风险。一般情况下,建议使用同一个大版本进行项目开发,如果确定需要进行大版本切换,请务必做完备的功能测试。
(本文完 c-1-d e-a-20200628)
技术公众号(《Throwable文摘》,id:throwable-doge),不定期推送笔者原创技术文章(绝不抄袭或者转载):
作者:Throwable
出处:https://www.cnblogs.com/throwable/p/13206029.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
来源:博文来源于Throwable的个人博客Throwable's Blog
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构