SpringBoot 依赖管理

在这里插入图片描述

Spring Boot 依赖管理

在 Spring Boot 中,依赖管理是通过 Maven 或 Gradle 进行管理的。Spring Boot 提供了一种简化的方式来管理和引入依赖项,使得构建和管理项目变得更加容易。下面是一些关于 Spring Boot 依赖管理的基本信息和示例:

使用 Maven 进行 Spring Boot 依赖管理:

在 Maven 项目中,您可以通过在 pom.xml 文件中添加 Spring Boot 的 starter 依赖来引入所需的库。Spring Boot 提供了一系列预定义的 starter 依赖,它们包含了常用的库和配置,使您能够更容易地构建特定类型的应用。

以下是一个简单的 Spring Boot Maven 项目的 pom.xml 示例:

<project>
    <groupId>com.example</groupId>
    <artifactId>spring-boot-demo</artifactId>
    <version>1.0.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
    </parent>
    <dependencies>
        <!-- Spring Boot Starter Web: 包含 Spring Web MVC 和其他相关库 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 其他依赖... -->
    </dependencies>
</project>

在上述示例中,spring-boot-starter-web 是一个常见的 Spring Boot starter 依赖,用于构建 Web 应用。您可以根据您的项目需求添加其他 starter 依赖或自定义依赖。

无论您选择使用 Maven 还是 Gradle,Spring Boot 都会自动处理依赖项的版本管理,确保您的项目的各个库的版本兼容性。当您添加新的 starter 依赖时,Spring Boot 会自动解决依赖关系并引入所需的库。

1. 父项目做依赖管理

无需关注版本号,自动版本仲裁机制

<!-- 依赖管理 -->
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
</parent>

<!-- spring-boot-starter-parent 的父项目 -->
<!-- 几乎声明了所有开发中常用的依赖的版本号 -->
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.4.RELEASE</version>
  </parent>

2. 修改默认版本号
<!-- 查看spring-boot-dependencies里面规定当前依赖的版本用的 key -->
<!-- 在当前项目里面重写配置 就近原则 -->
    <properties>
        <mysql.version>5.1.43</mysql.version>
    </properties>
3. starter 场景启动器
<!-- 包含 spring-web、webmvc、tomcat、json、spring-boot-starter 等依赖 -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<!-- SpringBoot 官方所有支持的场景文档: https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter -->
<!-- 见到的  *-spring-boot-starter 格式 为第三方为我们提供的简化开发的场景启动器 -->

<!-- 所有场景启动器最底层的依赖 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
  <version>2.3.4.RELEASE</version>
  <scope>compile</scope>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

羽露风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值