Spring Boot 多模块开发, 子模块的bean调用问题及解决方法

本文介绍了Spring Boot如何构建多模块工程,详细讲解了父模块和子模块的pom.xml配置,以及在多模块中解决Bean调用问题的方法,包括在Web模块中使用API模块的代码示例和解决组件扫描问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Boot 多模块工程

1、新建一个maven工程

新建maven工程

工程名 :spring-boot-sample-project

spring-boot-sample-project

删除多余的文件,只留spring-boot-sample-project工程中的pom.xml文件

父工程pom.xml文件

2、在spring-boot-sample-project工程中增加子模块

在这里插入图片描述

新增:spring-boot-sample-api 模块,注意图中2位置的包名com.example.sample.api,因为后面会涉及到多模块之间的相互调用,为了方便扫描,将包名统一成com.example.sample.模块的名

在这里插入图片描述

同理,新增:spring-boot-sample-web模块,选择Spring Boot web开发所需要的依赖的,这里我勾选了Spring Web 和 Lombok

在这里插入图片描述

最后整个工程目录如下:

在这里插入图片描述

3、配置pom.xml 文件

3.1、父类工程spring-boot-sample-project的pom.xml

1、首先是父类工程的基本信息

<!-- 基本信息 -->
    <description>Spring Boot 多模块构建</description>
    <modelVersion>4.0.0</modelVersion>
    <name>spring-boot-sample</name>
    <packaging>pom</packaging>

    <!-- 项目说明:这里作为聚合工程的父工程 -->
    <groupId>com.example</groupId>
    <artifactId>spring-boot-sample-project</artifactId>
    <version>1.0-SNAPSHOT</version>

2、因为整个工程都会依赖于Spring Boot,因此spring-boot-sample-project需要继承Spring Boot父类

<!-- 继承说明:这里继承SpringBoot提供的父工程 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

3、声明子模块,创建好的子模块放到modules

<!-- 模块说明:这里声明多个子模块 -->
    <modules>
        <module>spring-boot-sample-api</module>
        <module>spring-boot-sample-web</module>
    </modules>

4 、版本管理,可以将子模块等需要版本管理的依赖放到dependencyManagement进行版本管理

 <!-- 版本说明:这里统一管理依赖的版本号 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>spring-boot-sample-api</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>spring-boot-sample-web</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

5、其他

3.1.1、完整的父工程pom.xml配置代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!-- 基本信息 -->
    <description>Spring Boot 多模块构建</description>
    <modelVersion>4.0.0</modelVersion>
    <name>spring-boot-sample-project</name>
    <packaging>pom</packaging>

    <!-- 项目说明:这里作为聚合工程的父工程 -->
    <groupId>com.example</groupId>
    <artifactId>spring-boot-sample-project</artifactId>
    <version>1.0-SNAPSHOT
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值