学透Spring Boot — 005. 深入理解 Spring Boot Starter 依赖管理

news2024/10/7 16:18:08

前面的文章直观的展示了,使用Spring Boot 集成 Hibernate 和手动集成 Hibernate 之间差距。

一个比喻

工作中使用过Spring Boot一段时间后,我越来越感觉到Spring Boot Starter带来的便利性。

使用手动集成 Hibernate 就像去电脑城配电脑:

  • 你不仅要找齐所有必备的部件,比如CPU、内存、主板、显卡等等:
  • 你还要保证各个零部件是相互兼容的,比如太老的主板不支持某些显卡等等
    在这里插入图片描述这样导致,组装电脑的成本很大,费时费力!这和我们开发中手动集成某个模块非常类似。

但是如果使用 Spring Boot Starter,就像我们买电脑时,直接买笔记本电脑或者一体机。
用一个词形容就是 开箱即用
在这里插入图片描述

Starter的依赖管理

Spring Boot Starter 是Spring Boot 中非常重要的概念,它是一种提供依赖项的方式,可以帮助开发人员快速集成各种第三方库和框架。

它应该包含两方面的内容:

  1. 依赖管理
  2. 自动配置

今天我们主要讨论的是其中的 依赖管理 部分。

前面博文说到,只要在在pom.xml中加一个依赖项,我们就几乎把持久层的所有依赖都自动加到我们的项目了(当然你还需要加数据库驱动):

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

在这里插入图片描述

但是这是怎么做到的呢?今天我们就来一探究竟。

什么是 Maven POM

pom.xml 这个我们经常打交道的文件,今天我们来深入学习它。

POM ( Project Object Model,项目对象模型 ) 是 Maven 工程的基本工作单元,是一个XML文件,包含了项目的基本信息,用于描述项目如何构建,声明项目依赖,等等。

POM 中做很多很多事,这些事都是和项目构建相关的,比如:

  • 声明和管理项目依赖
  • 配置各种插件
  • 执行目标
  • 项目构建 profile
  • 指定项目版本

当然我们最最常用的是声明和管理项目用到的各种依赖。
它的主要结构如下:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.companyname.project-group</groupId>
    <artifactId>project</artifactId>
    <version>1.0</version>
    
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
    </dependencies>
</project>

这里只是列出了最核心的元素,也是和我们本文相关的。
pom.xml 文件详解请看这里

其中的 <dependencies> 是我们重点关注的对象,它定义了项目相关的所有依赖。

这些依赖组成了项目构建过程中的一个个环节。它们自动从项目定义的仓库中下载(比如你配置了阿里云仓库,或者默认的maven中央仓库)。

什么是 Maven 依赖传递

想象一下,在很久很久以前,在没有Maven等构建工具时,我们要手动下载三方jar包,然后放到我们的项目的lib目录下。

如果这些三方的jar包是这种依赖关系时。

  • 如果我们的项目用到了A包,那么我们需要手动下载A、B、C、D、E包到lib目录
  • 如果我们项目用到了B包,那么我们需要手动下载B、C、E包到lib目录下
  • ……
    在这里插入图片描述
    好在,Maven 引入了一个依赖传递传递机制,帮我们大大简化了这个问题。

有了这个机制,当我们项目中用到A.jar时,我们只需要在pom.xml的声明A,至于它依赖的其它jar包,都会被自动的依赖进来,包括它依赖的依赖。

当然传递依赖会引发一些问题,关于排除依赖等不在本文的范围。

Spring Boot Starter的依赖

通过学习上面的知识,我们就很容易理解为什么声明一个依赖,就会自动把所有的持久层依赖都自动加入进来了。

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

平时我们声明其它依赖时,都需要指定版本,但是为什么可以不指定?因为默认不指定时,它会根据项目的父级依赖管理来确定依赖项的版本。
我们在pom.xml的上面部分可以看到父级项目

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

可以看到父级项目版本是3.2.4, 所以相当于:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>3.2.4</version>
</dependency>

这个依赖我们可以按住ctrl键 加鼠标左键,可以跳转到它的pom.xml中去
在这里插入图片描述
这个pom.xml文件在你的本地的maven 仓库的某个目录下,比如:
在这里插入图片描述

这个包和普通我们依赖的包看上去并没有没什么区别。

要说最大的区别,就是它虽然是一个jar,但是并不包含java代码,在IDEA的依赖中可以看到,里面只有一些声明文件
在这里插入图片描述
这是因为,这个jar存在的唯一意义,就是定义一组依赖。

所以我们重点看 spring-boot-starter-data-jpa-3.2.4.pom 文件的 dependencies:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    <version>3.2.4</version>
    <scope>compile</scope>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <version>3.2.4</version>
    <scope>compile</scope>
  </dependency>
  <dependency>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>6.4.4.Final</version>
    <scope>compile</scope>
  </dependency>
  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>3.2.4</version>
    <scope>compile</scope>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>6.1.5</version>
    <scope>compile</scope>
  </dependency>
</dependencies>

可以看到,它依赖5个包:

  • spring-boot-starter-aop
  • spring-boot-starter-jdbc
  • hibernate-core
  • spring-data-jpa
  • spring-aspects

这五个包,前面两个也是 starter,后面3个是普通的包。

然后这些依赖又会递归的把它们的依赖包含进来。

最终的依赖图

根据pom.xml中的依赖传递,我们可以得到一个依赖关系图
在这里插入图片描述

是不是非常眼熟呢?对的,就是我们前文中贴出来的IDEA生成的依赖:
在这里插入图片描述

Spring Boot 做了什么

前面可以看到,定义一个spring-boot-starter-data-jpa 依赖就把我们持久层的所有依赖都搞定,其实是 maven 做到的,确切的说是maven的依赖传递机制完成的,并不是Spring Boot的新特性。

那么Spring Boot在这里做了什么呢?

其实它主要的是任务是打包,它按一般的场景划分成多个模块,然后定义每个模块下的一组依赖。就比如前面的 spring-boot-starter-data-jpa-3.2.4 它定义了:

  • spring-boot-starter-aop-3.2.4
  • spring-boot-starter-jdbc-3.2.4
  • hibernate-core-6.4.4.Final
  • spring-data-jpa-3.2.4
  • spring-aspects-6.1.5

它不仅指示某个模块下,哪些依赖是必须的
还指定了依赖具体的版本号

这些版本的依赖都是经 Spring Boot 官方测试过的,我们可以放心的使用。

Spring Boot 官方提供的Starter

Spring Boot 官方提供了几十个开箱即用的Starter,几乎涵盖了所有项目常见的场景。

有了这些starter,你再也不用到处找演示文档了,不用再一个个配置必备的依赖了,也不用担心版本兼容问题。使用某个模块,直接使用这个模块的starter即可。

我们可以把这些starter 理解成套餐,大部分项目都会这么用spring-web, 这么用redis等等,Spring Boot已经帮我们打包好了,你直接享用即可。
在这里插入图片描述

1. 应用相关的Starter
NameDescription
spring-boot-starterCore starter, including auto-configuration support, logging and YAML
spring-boot-starter-activemqStarter for JMS messaging using Apache ActiveMQ
spring-boot-starter-amqpStarter for using Spring AMQP and Rabbit MQ
spring-boot-starter-aopStarter for aspect-oriented programming with Spring AOP and AspectJ
spring-boot-starter-artemisStarter for JMS messaging using Apache Artemis
spring-boot-starter-batchStarter for using Spring Batch
spring-boot-starter-cacheStarter for using Spring Framework’s caching support
spring-boot-starter-data-cassandraStarter for using Cassandra distributed database and Spring Data Cassandra
spring-boot-starter-data-cassandra-reactiveStarter for using Cassandra distributed database and Spring Data Cassandra Reactive
spring-boot-starter-data-couchbaseStarter for using Couchbase document-oriented database and Spring Data Couchbase
spring-boot-starter-data-couchbase-reactiveStarter for using Couchbase document-oriented database and Spring Data Couchbase Reactive
spring-boot-starter-data-elasticsearchStarter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch
spring-boot-starter-data-jdbcStarter for using Spring Data JDBC
spring-boot-starter-data-jpaStarter for using Spring Data JPA with Hibernate
spring-boot-starter-data-ldapStarter for using Spring Data LDAP
spring-boot-starter-data-mongodbStarter for using MongoDB document-oriented database and Spring Data MongoDB
spring-boot-starter-data-mongodb-reactiveStarter for using MongoDB document-oriented database and Spring Data MongoDB Reactive
spring-boot-starter-data-neo4jStarter for using Neo4j graph database and Spring Data Neo4j
spring-boot-starter-data-r2dbcStarter for using Spring Data R2DBC
spring-boot-starter-data-redisStarter for using Redis key-value data store with Spring Data Redis and the Lettuce client
spring-boot-starter-data-redis-reactiveStarter for using Redis key-value data store with Spring Data Redis reactive and the Lettuce client
spring-boot-starter-data-restStarter for exposing Spring Data repositories over REST using Spring Data REST and Spring MVC
spring-boot-starter-freemarkerStarter for building MVC web applications using FreeMarker views
spring-boot-starter-graphqlStarter for building GraphQL applications with Spring GraphQL
spring-boot-starter-groovy-templatesStarter for building MVC web applications using Groovy Templates views
spring-boot-starter-hateoasStarter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS
spring-boot-starter-integrationStarter for using Spring Integration
spring-boot-starter-jdbcStarter for using JDBC with the HikariCP connection pool
spring-boot-starter-jerseyStarter for building RESTful web applications using JAX-RS and Jersey. An alternative to spring-boot-starter-web
spring-boot-starter-jooqStarter for using jOOQ to access SQL databases with JDBC. An alternative to spring-boot-starter-data-jpa or spring-boot-starter-jdbc
spring-boot-starter-jsonStarter for reading and writing json
spring-boot-starter-mailStarter for using Java Mail and Spring Framework’s email sending support
spring-boot-starter-mustacheStarter for building web applications using Mustache views
spring-boot-starter-oauth2-authorization-serverStarter for using Spring Authorization Server features
spring-boot-starter-oauth2-clientStarter for using Spring Security’s OAuth2/OpenID Connect client features
spring-boot-starter-oauth2-resource-serverStarter for using Spring Security’s OAuth2 resource server features
spring-boot-starter-pulsarStarter for using Spring for Apache Pulsar
spring-boot-starter-pulsar-reactiveStarter for using Spring for Apache Pulsar Reactive
spring-boot-starter-quartzStarter for using the Quartz scheduler
spring-boot-starter-rsocketStarter for building RSocket clients and servers
spring-boot-starter-securityStarter for using Spring Security
spring-boot-starter-testStarter for testing Spring Boot applications with libraries including JUnit Jupiter, Hamcrest and Mockito
spring-boot-starter-thymeleafStarter for building MVC web applications using Thymeleaf views
spring-boot-starter-validationStarter for using Java Bean Validation with Hibernate Validator
spring-boot-starter-webStarter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container
spring-boot-starter-web-servicesStarter for using Spring Web Services
spring-boot-starter-webfluxStarter for building WebFlux applications using Spring Framework’s Reactive Web support
spring-boot-starter-websocketStarter for building WebSocket applications using Spring Framework’s MVC WebSocket support
2. 生产相关的starter
NameDescription
spring-boot-starter-actuatorStarter for using Spring Boot’s Actuator which provides production-ready features to help you monitor and manage your application
3. 技术相关的starter
NameDescription
spring-boot-starter-jettyStarter for using Jetty as the embedded servlet container. An alternative to spring-boot-starter-tomcat
spring-boot-starter-log4j2Starter for using Log4j2 for logging. An alternative to spring-boot-starter-logging
spring-boot-starter-loggingStarter for logging using Logback. Default logging starter
spring-boot-starter-reactor-nettyStarter for using Reactor Netty as the embedded reactive HTTP server.
spring-boot-starter-tomcatStarter for using Tomcat as the embedded servlet container. Default servlet container starter used by spring-boot-starter-web
spring-boot-starter-undertowStarter for using Undertow as the embedded servlet container. An alternative to spring-boot-starter-tomcat

总结

通过本文,我们深入学习了 Spring Boot Starter 依赖管理背后的 Maven的传递依赖机制是如何工作的,以及 Spring Boot 提供了哪些开箱即用的starter。

这是Spring Boot 系列专栏的第5篇,下一篇我们将深入学习Spring Boot的自动配置,关注我,和我一起学透 Spring Boot.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1578931.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

二叉树的遍历的递归与非递归算法

一.二叉树的遍历&#xff1a; 按照一定规律对二叉树的每个结点进行访问且仅访问一次&#xff1b; 这里的访问&#xff1a;可以是计算二叉树中的结点数据&#xff0c;打印该结点的信息&#xff0c;也可以是对结点进行的任何其它操作&#xff01; 为什么需要遍历二叉树&#x…

EditPlus来啦(免费使用!)

hello&#xff0c;我是小索奇 今天推荐一款编辑器&#xff0c;是索奇学习JavaSE时入手滴&#xff0c;非常好用哈&#xff0c;小索奇还是通过老杜-杜老师入手滴&#xff0c;相信很多人也是通过老杜认识嘞&#xff0c;来寻找破解版或者准备入手这个间接使用的编辑器~ EditPlus是…

在Ubuntu上搭建Prometheus + Grafana监控系统

1.Prometheus 部署 从官网下载页面找到最新的二进制文件下载 cd ~ curl -LO https://github.com/prometheus/prometheus/releases/download/v2.51.1/prometheus-2.51.1.linux-amd64.tar.gz将文件解压到指定目录 tar xf prometheus-2.51.1.linux-amd64.tar.gz -C /usr/local为…

⼿机客户端画K线图流程

优质博文&#xff1a;IT-BLOG-CN 一、什么是K线流程 K线图是一种用于展示金融市场价格走势的图表。它通常由四个关键价格点组成&#xff0c;即开盘价、收盘价、最高价和最低价。K线图的流程可以简单概括为以下几个步骤&#xff1a; 【1】收集数据&#xff1a; 首先&#xff0c…

Oracle 正则表达式

一、Oracle 正则表达式相关函数 (1) regexp_like &#xff1a;同 like 功能相似&#xff08;模糊 匹配&#xff09; (2) regexp_instr &#xff1a;同 instr 功能相似&#xff08;返回字符所在 下标&#xff09; (3) regexp_substr &#xff1a; 同 substr 功能相似&…

虚拟网络设备与Linux网络协议栈

在现代计算环境中&#xff0c;虚拟网络设备在实现灵活的网络配置和隔离方面发挥了至关重要的作用&#x1f527;&#xff0c;特别是在容器化和虚拟化技术广泛应用的今天&#x1f310;。而Linux网络协议栈则是操作系统处理网络通信的核心&#x1f4bb;&#xff0c;它支持广泛的协…

点击上传文件

一、页面样式&#xff1a; &#xff08;1&#xff09;点击前&#xff1a; &#xff08;2&#xff09;点击后&#xff1a; 设计&#xff1a;①自定义elementPlus图标&#xff1b;②使用Tooltip实现鼠标悬浮按钮上出现文字提示&#xff1b;③上传与更换的切换样式&#xff1b;…

全栈开发医疗小程序 SpringBoot2.X + Vue + UniAPP 带源码

看到好多坛友都在求SpringBoot2.X Vue UniAPP&#xff0c;全栈开发医疗小程序 – 带源码课件&#xff0c;我看了一下&#xff0c;要么链接过期&#xff0c;要么课件有压缩密码。特意整理了一份分享给大家&#xff0c;个人认为还是比较全面的。希望对大家有所帮助&#xff01;…

云计算(五)—— OpenStack基础环境配置与API使用

OpenStack基础环境配置与API使用 项目实训一 【实训题目】 使用cURL命令获取实例列表 【实训目的】 理解OpenStack的身份认证和API请求流程。 【实训准备】 &#xff08;1&#xff09;复习OpenStack的认证与API请求流程的相关内容。 &#xff08;2&#xff09;熟悉cURL…

openGauss学习笔记-258 openGauss性能调优-使用Plan Hint进行调优-指定子查询不展开的Hint

文章目录 openGauss学习笔记-258 openGauss性能调优-使用Plan Hint进行调优-指定子查询不展开的Hint258.1 功能描述258.2 语法格式258.3 示例 openGauss学习笔记-258 openGauss性能调优-使用Plan Hint进行调优-指定子查询不展开的Hint 258.1 功能描述 数据库在对查询进行逻辑…

vulhub中Apache Solr Velocity 注入远程命令执行漏洞复现 (CVE-2019-17558)

Apache Solr 是一个开源的搜索服务器。 在其 5.0.0 到 8.3.1版本中&#xff0c;用户可以注入自定义模板&#xff0c;通过Velocity模板语言执行任意命令。 访问http://your-ip:8983即可查看到一个无需权限的Apache Solr服务。 1.默认情况下params.resource.loader.enabled配置…

OpenCV单通道图像按像素成倍比例放大(无高斯平滑处理)

OpenCV中的resize函数可以对图像做任意比例的放大(/缩小)处理&#xff0c;该处理过程会对图像做高斯模糊化以保证图像在进行放大&#xff08;/缩小&#xff09;后尽可能保留源图像所展现的具体内容&#xff08;消除固定频率插值/采样带来的香农采样信息损失&#xff09;&#x…

7款公司电脑监控软件

7款公司电脑监控软件 研究证明&#xff0c;人们在家办公的效率比在办公室办公的效率低一半&#xff0c;其中原因是缺少监督&#xff0c;即便在公司办公&#xff0c;还存在员工偷闲的时刻&#xff0c;比如聊天、浏览无关网站、看剧、炒股等&#xff0c;企业想提高员工的工作效率…

GFS分布式 文件系统

一、GFS的概念 文件存储分为nfs、lvm、raid 对象存储分为GFS、CEPH、fastDFS&#xff08;分布式文件存储&#xff09;NAS OSS S3 switch OSS 属于阿里云 通过URL 链接 S3属于亚马逊通过URL链接 1.1 GFS简介 开源的分布式文件系统&#xff0c;由存储服务器、客户端…

工地安全监测识别摄像机

工地安全监测识别摄像机是一种在建筑工地和施工现场广泛使用的智能监控设备&#xff0c;主要用于监测施工过程中可能出现的安全隐患和违规行为&#xff0c;以确保工地人员和设备的安全。通过高清摄像头、智能算法和远程监控系统的结合&#xff0c;该摄像机可以实时监测工地各个…

基于支持 GPT 的服务的初创公司

Kafkai&#xff1a;多语言长篇内容生成&#xff0c;AI写作的新趋势 介绍 随着生成式预训练 Transformer (GPT) 的出现&#xff0c;技术世界正在见证范式转变。 这种人工智能驱动的创新不仅仅是一种转瞬即逝的趋势&#xff0c;而是一种趋势。 它已成为科技行业的基石&#xff0c…

使用新版FLIR (FLIR_ADAS_v2) 数据集创建yolo格式数据集(目标检测)

FLIR在2022.1.19发布了新版的FLIR_ADAS_v2&#xff0c;有着更多的类别和数量更丰富的图像。数据集同步注释热图像和无注释RGB图像供参考。本文章主要介绍如何使用FLIR_ADAS_v2中的rgb图像和thermal图像来制作yolo格式数据集。 1.官方数据集下载&#xff1a;FLIR_ADAS_v2数据集…

支持向量机(SVM)白话之个人理解(学习记录)

本文仅有文字理解部分&#xff0c;没有相应的数学公式推导过程&#xff0c;便于新手理解。 一、什么是支持向量机 首先我们看下面这张图&#xff0c;在图中圆形和三角形分别代表不同的数据类型&#xff0c;如何画出一条直线使两者能够显著地区分开来呢&#xff1f; 答案可以多…

PDF锐化

PDF Shaper Ultimate(pdf转图片) 编辑->添加文件->选中一个要处理的pdf 操作->转换->PDF转为图片 ComicEnhancerPro设置(把图片锐化) PDF Shaper Ultimate(图片转pdf) 编辑-添加图片->选中所有锐化处理后的图片 转换->图片转为pdf&#xff08;会把所有图…

通用CI/CD软件平台TeamCity 2024.03发布——支持HashiCorp Vault插件

TeamCity是一个通用的 CI/CD 软件平台&#xff0c;可以实现灵活的工作流、协作和开发做法。我们的解决方案将帮助在您的 DevOps 流程中成功实现持续集成、持续交付和持续部署。 获取TeamCity 2024.03正式版试用(Q技术交流&#xff1a;909157416&#xff09; 具体更新详情如下…