【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 客户端等。
相关推荐
中国胖子风清扬14 小时前
Rust 日志库完全指南:从入门到精通
spring boot·后端·rust·学习方法·logback
玉衡子14 小时前
MySQL基础架构全面解析
数据库·后端
郭京京14 小时前
goweb内置的 net/http 包
后端·go
dylan_QAQ14 小时前
Java转Go全过程06-工程管理
java·后端·go
用户40993225021214 小时前
如何用FastAPI玩转多模块测试与异步任务,让代码不再“闹脾气”?
后端·ai编程·trae
xiaogg367815 小时前
springboot rabbitmq 延时队列消息确认收货订单已完成
spring boot·rabbitmq·java-rabbitmq
麦兜*15 小时前
MongoDB 6.0 新特性解读:时间序列集合与加密查询
数据库·spring boot·mongodb·spring·spring cloud·系统架构
考虑考虑15 小时前
Postgerssql格式化时间
数据库·后端·postgresql
依稀i12315 小时前
Spring Boot + MySQL 创建超级管理员
spring boot·mysql
千里码aicood15 小时前
【springboot+vue】党员党建活动管理平台(源码+文档+调试+基础修改+答疑)
java·数据库·spring boot