当前位置: 首页 > news >正文

Spring Boot Starter Parent介绍

引言

spring-boot-starter-parent 是一个特殊的项目,为基于 Spring Boot 的应用程序提供默认配置和默认依赖。

在本 Spring Boot 教程中,我们将深入了解所有 Spring Boot 项目内部使用的 spring-boot-starter-parent 依赖项。我们将探讨此依赖项所提供的所有配置选项,以及如何根据需要覆盖这些默认配置。

快速引入

如果你的工程是基于Maven构建的,可以在pom.xml中按以下方式引入:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.1.2</version><relativePath/> <!-- lookup parent from repository -->
</parent>

类似的,如果是基于Gradle构建,可以参考按以下方式在build.gradle中引入:

plugins {id 'java'id 'org.springframework.boot' version '3.1.2'id 'io.spring.dependency-management' version '1.1.2'
}dependencies {implementation 'org.springframework.boot:spring-boot-starter'testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

什么是Spring Boot Starter Parent项目

Spring Boot 旨在简化构建 Spring 应用程序的过程,同时尽量减少配置工作量。为此,诞生了 spring-boot-starter-parent父项目,为基于 Spring Boot 的应用程序提供默认配置和依赖项管理。

使用 spring-boot-starter-parent 作为父项目,可以确保应用程序遵循 Spring Boot 推荐的最佳实践,并且能够轻松地利用 Spring Boot 提供的各种特性。

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.howtodoinjava</groupId><artifactId>spring-webmvc</artifactId><packaging>jar</packaging><version>0.0.1-SNAPSHOT</version><name>spring-webmvc Maven Webapp</name><url>https://howtodoinjava.com</url><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.1.2</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>......</dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

当我们创建一个 Spring Boot 项目时,我们使用 spring-boot-starter-parent 作为我们项目的 pom.xml 或 build.gradle 的父级。添加它之后,我们的项目会从这个父项目中继承默认的构建、依赖项和配置,因此我们不必手动指定它们。
在这里插入图片描述
通过使用 spring-boot-starter-parent,我们可以快速启动 Spring Boot 应用程序开发并减少设置常见配置和依赖项所需的工作量。

Spring Boot Dependencies 项目介绍

“Spring Boot Starter Parent”项目进一步扩展了 spring-boot-dependencies 项目,spring-boot-dependencies 项目的作用包括:

  • 统一依赖版本管理:为 Spring Boot 应用程序中常用的依赖项提供统一的版本管理,确保所有依赖项的版本都是兼容且经过测试的。
  • 减少版本冲突:通过集中管理所有依赖项的版本,避免了因版本不一致导致的问题。
  • 简化依赖声明:在项目中引用依赖项时,无需指定版本号,因为这些信息已经由 spring-boot-dependencies 统一管理。
<properties><activemq.version>5.18.2</activemq.version><angus-mail.version>1.1.0</angus-mail.version><artemis.version>2.28.0</artemis.version><aspectj.version>1.9.19</aspectj.version><assertj.version>3.24.2</assertj.version><awaitility.version>4.2.0</awaitility.version>......
</properties>

我们可以参考 spring-boot-dependencies项目的最新版本,并检查其 pom.xml 以获取所有依赖项使用的最新版本。

项目依赖管理

引入默认版本的依赖

一旦我们在项目中声明了 spring-boot-starter-parent,我们就可以引入其中的任何依赖项,只需在 dependencies 标签中声明它们即可。这些依赖项已经在 spring-boot-starter-parent 中指定了默认版本,因此无需在项目中再次指定版本号。

<dependencies><!-- 引入 Spring Web Starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 其他依赖项 -->
</dependencies>

由于 spring-boot-starter-parent 已经管理了所有依赖项的版本,因此上述代码片段中不需要指定版本号。这样可以简化依赖管理,并确保所有依赖项版本的一致性。

这种方式不仅简化了依赖项的管理,还确保了项目使用的是经过测试和验证的依赖项组合。

引入不同版本的依赖

要包含具有不同版本的依赖项,我们可以在 dependencyManagement 部分中指定依赖项及其版本。这样做可以覆盖 spring-boot-starter-parent 中默认提供的版本,同时仍然享受统一版本管理带来的便利。

<dependencyManagement><dependencies><!-- 覆盖特定依赖项的版本 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.7.4</version> <!-- 特定版本 --></dependency></dependencies>
</dependencyManagement>

在这个例子中,spring-boot-starter-web 的版本在 dependencyManagement 部分中被指定为 2.7.4。这样,在实际使用的依赖项中引用 spring-boot-starter-web 时,就可以使用这个指定的版本,而不是 spring-boot-starter-parent 中默认的版本。

通过这种方式,你可以灵活地覆盖或指定特定依赖项的版本,同时仍然保持项目依赖项的整体一致性。

或者,我们可以在属性部分覆盖所包含库的版本号。

<properties><junit.version>4.13.2</junit.version><junit-jupiter.version>5.9.3</junit-jupiter.version>
</properties>

总结

本 Spring Boot 教程讨论了特殊项目 spring-boot-starter-parent 和 spring-boot-dependencies,它们之间的继承关系,以及它们在我们创建的任何 Spring Boot 项目中所带来的好处。我们还学习了如何包含默认依赖项以及如何覆盖这些依赖项的版本。

通过使用 spring-boot-starter-parent 和 spring-boot-dependencies,我们可以:

  • 简化依赖管理:避免在项目中显式指定每个依赖项的版本号,从而减少版本冲突的风险。
  • 统一版本控制:确保所有依赖项的版本都是统一管理的,这有助于维护项目的稳定性和兼容性。
  • 提高开发效率:通过预配置的默认设置和插件配置,可以更快地搭建项目基础结构。
  • 灵活覆盖版本:在需要时,可以在 dependencyManagement 部分中覆盖特定依赖项的版本,以适应特定项目需求。

总之,这些工具和最佳实践帮助开发者更容易地构建健壮且易于维护的 Spring Boot 应用程序。希望本教程对您的学习之旅有所帮助!

祝您学习愉快!!

http://www.lryc.cn/news/456942.html

相关文章:

  • 【含开题报告+文档+PPT+源码】基于SpringBoot乡村助农益农平台的设计与实现
  • 数据中心运维挑战:性能监控的困境与智能化解决方案的探寻
  • 基于SSM的民宿管理系统【附源码】
  • 显卡 3090 vs v100
  • 怎么在单片机裸机程序中移植EasyLogger?
  • C/C++解析文件名和目录路径
  • Git 基本命令行操作
  • 【Rust练习】17.泛型
  • java脚手架系列4--测试用例、拦截器
  • 论文推荐 |【Agent】自动化Agent设计系统
  • Linux操作系统提供了五种主要的IO(输入/输出)模型
  • 基于深度学习的花卉识别系统
  • 【斯坦福CS144】Lab0
  • 关于Mybatis中,IPage<PO>转换成IPage<VO>的问题
  • 使用idea和vecode创建vue项目并启动(超详细)
  • C#|.net core 基础 - 删除字符串最后一个字符的七大类N种实现方式
  • 成都睿明智科技有限公司怎么样靠谱吗?
  • docker简述
  • 第27周:Transformer实战:文本分类
  • 在QT中将Widget提升为自定义的Widget后,无法设置Widget的背景颜色问题解决方法
  • 【学习笔记】手写一个简单的 Spring IOC
  • 日记学习小迪安全27
  • 【React】类组件和函数组件
  • Spring Boot应用开发
  • mysql事务使用和事务隔离级别与sqlserver的比较
  • 双光吊舱图像采集详解!
  • 1688商品详情关键词数据-API
  • vue 的属性绑定
  • 【附源码】Python :打家劫舍
  • YOLO11改进 | 注意力机制| 对小目标友好的BiFormer【CVPR2023】