spring-boot-starter-parent与spring-boot-dependencies关系

前言

一开始使用springboot时,经常会在springboot的pom文件中看见spring-boot-starter-parent、spring-boot-dependencies、spring-boot-starter-web、spring-boot-starter等依赖,他们之间的关系是什么样的,通过这篇文章梳理一下。

常见依赖的pom:

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

<dependency>  
	<groupId>org.springframework.boot</groupId>  
	<artifactId>spring-boot-dependencies</artifactId>  
</dependency>

<dependency>  
	<groupId>org.springframework.boot</groupId>  
	<artifactId>spring-boot-starter-web</artifactId>  
</dependency>

<dependency>  
	<groupId>org.springframework.boot</groupId>  
	<artifactId>spring-boot-starter</artifactId>  
</dependency>

结论:先说结论,在创建spring boot工程时,spring-boot-starter-parent 和 spring-boot-dependencies是二选一的关系,在pom中引入其中一个就可以了。

spring-boot-starter-parent

使用spring-boot-starter-parent通过maven继承使用,方式如图,spring-boot-starter-parent会对常见的springboot使用到的包进行统一管理。在本工程中使用的时候,只需要按需根据groupId和artifactId引入,无需填写具体的版本号。

![[Pasted image 20231126171258.png]]

如下图,点开spring-boot-starter-parent的实现,其实能发现spring-boot-starter-parent 也是通过继承 spring-boot-dependencies而来。
在这里插入图片描述

spring-boot-dependencies

spring-boot-starter-parent是通过继承的方式引入springboot相关的依赖,spring-boot-dependencies也是对常用的springboot包进行统一管理,但是是通过将spring-boot-dependencies中统一管理的包直接引入到当前项目(scope使用import实现)。

在这里插入图片描述

starter和starter-web

spring-boot-starterspring-boot-starter-web 都是spring-boot-dependencies依赖管理下的具体的包。

  • spring-boot-starter:是Spring Boot框架的核心模块之一,它定义了一组通用的依赖项集合,用于简化Spring Boot应用程序的构建和配置。这些Starter模块提供了对各种功能的自动配置和集成,使得我们可以轻松地使用这些功能而无需手动进行繁琐的配置。

  • spring-boot-starter-web:是基于spring-boot-starter的一个扩展模块,它专门用于构建Web应用程序。它引入了Spring MVC、Tomcat服务器、Jackson JSON处理库等必要的依赖项,并通过自动配置来提供默认的Web应用程序上下文。它简化了构建Web应用程序的过程,使得我们可以更快速地搭建一个基于Spring MVC的Web项目。

结论spring-boot-starter-web是在spring-boot-starter的基础上针对Web应用程序提供的一个特定功能的Starter。如下图点开spring-boot-starter-web 的实现可以发现,spring-boot-starter-web已经引入了spring-boot-starter,所以在使用spring-boot-starter-web的时候不用单独再引用spring-boot-starter。

在这里插入图片描述

参考文章

  • https://blog.youkuaiyun.com/weixin_41979002/article/details/120678635
### Spring Cloud Dependencies vs Spring Boot Starter Parent #### 区别 Spring Boot Starter Parent 主要用于简化依赖管理和提供默认配置。通过继承 `spring-boot-starter-parent`,项目可以自动获得一系列合理的默认设置以及对常用库的版本管理[^1]。 另一方面,`spring-cloud-dependencies` 是一个 BOM (Bill of Materials),它定义了一组兼容的 Spring Cloud 库及其版本。这使得开发者可以在不显式声明版本的情况下引入多个 Spring Cloud 组件,并确保这些组件之间相互兼容[^3]。 #### 关系 两者都提供了依赖管理的功能,但是作用范围不同: - **Spring Boot Starter Parent** 提供了整个应用程序开发所需的基础依赖和配置支持; - **Spring Cloud Dependencies** 则专注于微服务架构下的分布式系统的构建,特别是那些由 Spring Cloud 生态圈提供的特性和服务发现、负载均衡等功能模块。 当一起使用时,通常会先继承 `spring-boot-starter-parent` 来获取基础的支持,然后再导入 `spring-cloud-dependencies` 作为 `<dependencyManagement>` 部分的内容来管理特定于云原生应用的需求[^2]。 #### 使用场景 对于大多数基于 Spring Boot 构建的应用程序来说,只需要继承 `spring-boot-starter-parent` 即可满足日常需求。然而,在涉及到复杂的微服务体系结构时,则应该考虑加入 `spring-cloud-dependencies` 以便更好地处理跨服务通信等问题。 如果希望自定义项目的父级 POM 或者已经有现有的企业级标准 POM 文件,可以选择仅使用 `spring-cloud-dependencies` 并将其 scope 设置为 import 进行依赖管理而不必强制继承官方提供的 starter parent。 ```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR8</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值