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

java_dependencies_父子模块引入依赖关系

问:
父模块中使用了 spring-boot-starter-web, 它的子模块,还需要引入吗?

答:
是否需要在子模块中 重复引入 spring-boot-starter-web,要根据实际使用方式来判断。下面是详细说明:


✅ 一、继承了父模块且不自定义依赖 —— 不用再引入

如果你的子模块的 pom.xml

  • 通过 <parent> 显式继承了父模块,
  • 且不使用 <dependencyManagement> 而是直接在父模块中声明了 <dependencies>

👉 那么:子模块默认会继承依赖可以不重复引入

示例:

🔹 父模块 pom.xml

<project><groupId>com.example</groupId><artifactId>parent</artifactId><version>1.0</version><packaging>pom</packaging><modules><module>module-a</module></modules><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
</project>

🔹 子模块 module-a/pom.xml

<project><parent><groupId>com.example</groupId><artifactId>parent</artifactId><version>1.0</version></parent><artifactId>module-a</artifactId>
</project>

这种情况下不需要重复引入 spring-boot-starter-web


⚠️ 二、父模块用了 <dependencyManagement> —— 子模块需要显式引入依赖

如果父模块的 pom.xml 只在 <dependencyManagement> 中做了版本统一管理:

<dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>3.2.5</version></dependency></dependencies>
</dependencyManagement>

那子模块就必须手动引入依赖(但不需要再写 version)

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

✅ 三、推荐做法

父模块是否包含依赖方式子模块是否需要引入?推荐
<dependencies>❌ 不需要⭐ 简单直观
<dependencyManagement>✅ 需要引入 artifactId更灵活,推荐中大型项目使用

如你项目是 Spring Boot 多模块项目,推荐将公共依赖放到父模块 <dependencies> 中,简化子模块维护。

如果你不确定你的继承方式,可以贴上父子 pom.xml 的关键部分,我可以直接帮你判断。

相关文章:

  • CompletableFuture+线程池使用案列
  • @Import原理与实战
  • 自定义protoc-gen-go生成Go结构体,统一字段命名与JSON标签风格
  • Go语言系统监控实战:gopsutil库全面解析与应用
  • 75Qt窗口_Qt窗口概览
  • Redis集群模式之主从复制模式(2)
  • 轻量级的Windows系统优化与个性化解决方案
  • 汽车车载软件平台化项目规模颗粒度选择的一些探讨
  • JS Day05
  • 幂等性的七大解决方案
  • 如何利用 audit2allow 工具调试SELinux日志
  • Spring类型转换器相关接口和实现原理
  • LLMs 系列科普文(8)
  • 免费批量PDF转Word工具
  • MyBatis原理剖析(一)
  • Vue ⑤-自定义指令 || 插槽
  • SCADA|RESTful学习,Apipost通过GET获取KingSCADA实时数据
  • LeetCode - 53. 最大子数组和
  • 哈佛总线架构是什么?
  • Linux(Centos 7.6)命令详解:which
  • 3d动画制作收费标准/seo网站系统
  • 网站用axure做的rp格式/网站推广平台搭建
  • 怎么在360网站做词条/crm软件
  • 石家庄网站app开发/上海十大营销策划公司
  • 教程网站搭建/淘宝代运营公司排名
  • 网站建设优化服务精英/杭州seo按天计费