spring-boot-starter-parent与spring-boot-dependencies

当构建Spring Boot聚合项目时,如果顶层项目不包含spring-boot-starter-parent,可以引入spring-boot-dependencies来实现依赖管理。通过在顶层pom的dependencyManagement部分添加spring-boot-dependencies,设置scope为import,可以享受与使用spring-boot-starter-parent相同的便利,而无需直接继承它。这样,子模块仍然能受益于Spring Boot的官方约定和依赖版本控制。

官方系统构建:https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems

使用maven构建聚合项目时,一般顶层的项目我们都没有引入parent包括spring-boot-starter-parent,很多时候顶层的项目就是其下子模块的parent,这个时候在子模块pom中是没法引入spring-boot-starter-parent的。这个时候,我们可以在顶层pom中引入spring-boot-dependencies

   <dependencyManagement>
        <dependencies>
             <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

不想使用spring-boot-starter-parent,你依然可以通过使用spring-boot-dependencies的scope=import利用依赖管理的便利。

我们通过分析spring-boot-starter-parent发现它继承了spring-boot-dependencies,同时spring-boot-starter-parent提供了很多官方约定的功能
在这里插入图片描述

### 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> ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值