【Spring Boot】Maven中引入 springboot 相关依赖的方式

文章目录

  • [Maven中引入 springboot 相关依赖的方式](#Maven中引入 springboot 相关依赖的方式)
    • [1. 不使用版本管理(不推荐)](#1. 不使用版本管理(不推荐))
    • 2、使用版本管理(推荐)
      • [2.1 继承 spring-boot-starter-parent](#2.1 继承 spring-boot-starter-parent)
      • [2.2 使用 spring-boot-dependencies + 自定义父工程](#2.2 使用 spring-boot-dependencies + 自定义父工程)
      • [2.3引入 spring-framework-bom](#2.3引入 spring-framework-bom)

Maven中引入 springboot 相关依赖的方式

1. 不使用版本管理(不推荐)

如果项目中没有统一版本管理,那么每个依赖都必须显式声明 <version>

示例:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.7.4</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <version>2.7.4</version>
</dependency>

⚡ 缺点: 手动指定,容易出错,不推荐。

2、使用版本管理(推荐)

2.1 继承 spring-boot-starter-parent

在 pom.xml 中直接继承:

xml 复制代码
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.4</version>
</parent>

然后添加依赖时,无需再写<version>

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.2 使用 spring-boot-dependencies + 自定义父工程

如果因为公司项目有自定义父 POM,又想用 Spring Boot 的统一版本管理,可以在 <dependencyManagement> 中导入:

xml 复制代码
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.7.4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

然后添加依赖时,同样无需再写<version>

2.3引入 spring-framework-bom

有时候,项目需要单独控制 Spring Framework 的各模块版本,比如在某些 JDK8 项目中,想让 Spring Framework 尽可能用最新兼容版本,这时候可以引入 spring-framework-bom,专门管理 Spring Framework 的依赖版本。

示例:

xml 复制代码
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>${spring.framework.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

然后就可以像下面这样引入 Spring Framework 的具体模块而不用单独写版本:

xml 复制代码
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
</dependency>

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

⚡ 注意:

  • spring-framework-bom 只管理 Spring Framework 本身(如 spring-core、spring-web、spring-context),不包括 Spring Boot 的 starter 或其他自动配置模块。
  • spring-boot-dependencies不仅管自己家的东西(上述 Spring Framework 本身),还顺便帮你管好了外部合作伙伴,比如:Jackson、Tomcat、MySQL 驱动、Redis 客户端等。
相关推荐
uzong17 分钟前
软件架构指南 Software Architecture Guide
后端
又是忙碌的一天17 分钟前
SpringBoot 创建及登录、拦截器
java·spring boot·后端
qq_5470261791 小时前
Maven 使用指南
java·maven
勇哥java实战分享1 小时前
短信平台 Pro 版本 ,比开源版本更强大
后端
学历真的很重要1 小时前
LangChain V1.0 Context Engineering(上下文工程)详细指南
人工智能·后端·学习·语言模型·面试·职场和发展·langchain
计算机毕设VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue二手家电管理系统(源码+数据库+文档)
vue.js·spring boot·后端·课程设计
上进小菜猪2 小时前
基于 YOLOv8 的智能杂草检测识别实战 [目标检测完整源码]
后端
韩师傅2 小时前
前端开发消亡史:AI也无法掩盖没有设计创造力的真相
前端·人工智能·后端
栈与堆3 小时前
LeetCode-1-两数之和
java·数据结构·后端·python·算法·leetcode·rust
superman超哥3 小时前
双端迭代器(DoubleEndedIterator):Rust双向遍历的优雅实现
开发语言·后端·rust·双端迭代器·rust双向遍历