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

搭建一个Spring Boot聚合项目

1. 创建父项目

  1. 打开IntelliJ IDEA,选择 New Project

  2. 在创建向导中选择 Maven,确保选中 Create from archetype,选择 org.apache.maven.archetypes:maven-archetype-quickstart

  3. 填写项目信息:

    • GroupIdcom.example(可以根据需求修改)

    • ArtifactIdspringboot-aggregator(父项目的名称)

    • Version1.0-SNAPSHOT(或其他版本号)

  4. 点击 Finish 完成父项目的创建。

2. 配置父项目的pom.xml

在父项目的pom.xml文件中,添加Spring Boot的父级POM和其他相关配置,以便管理所有子模块:

<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>springboot-aggregator</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/>
    </parent>

    <modules>
        <!-- 子模块会在这里列出 -->
    </modules>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- 统一管理依赖版本 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

3. 创建子模块

  1. 右键父项目(springboot-aggregator),选择 New > Module

  2. 选择 Maven 模块类型,然后填写模块名称(例如 service-userservice-productservice-order)。

  3. 完成模块创建后,在父项目的pom.xml文件中会自动添加相应的子模块<module>

4. 配置子模块的pom.xml

每个子模块的pom.xml继承父模块,并根据需要添加相关依赖。例如,service-user模块的pom.xml可以如下所示:

<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>service-user</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>springboot-aggregator</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

5. 编写Spring Boot应用程序代码

在每个子模块中编写具体的业务逻辑。例如,在service-user模块中,创建一个简单的Spring Boot应用程序:

package com.example.serviceuser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ServiceUserApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceUserApplication.class, args);
    }
}

 

6. 构建与运行

  1. 在父项目中执行 mvn clean install 来构建整个聚合项目。

  2. 运行时,可以在每个子模块中单独运行Spring Boot应用。例如,在service-user模块中右键点击ServiceUserApplication.java,选择 Run 来启动该模块的Spring Boot应用。

通过以上步骤,你就可以成功搭建一个Spring Boot聚合项目,帮助你更好地管理不同的功能模块。

 

相关文章:

  • 苍穹外卖day03
  • Redis之缓存更新策略
  • 10-常见笔试题-mk
  • 破解 MCP 认证难题方法深入了解考试内容
  • [MySQL] 索引
  • 使用Apache POI实现Java操作Office文件:从Excel、Word到PPT模板写入
  • 码界奇缘 Java 觉醒 后记 第二十二章 Epsilon无为秘境 - 寂静之地的内存试炼
  • 25软考中级*高项网课+历年真题+笔记+电子书+刷题【计算机软考】
  • C++——继承、权限对继承的影响
  • ubuntu学习day1
  • RuoYi-Vue升级为https访问-后端安装SSL证书(单台Linux服务器部署)
  • 图论基础理论
  • 低资源需求的大模型训练项目---调研0.5B大语言模型
  • 2025.04.13【Density 2d】| 基因表达数据可视化
  • Linux编程c/c++程序
  • 前端vue 项目px转为rem的自适应解决方案
  • open harmony多模组子系统分析
  • BM25、BGE以及text2vec-base-chinese的区别
  • [dp8_子数组] 乘积为正数的最长子数组长度 | 等差数列划分 | 最长湍流子数组
  • UE5角色状态机中跳跃落地移动衔接问题
  • 特朗普亲家有了新工作:美国驻法大使
  • 上海中心城区首条“定制化低空观光航线”启航,可提前一天提需求
  • 贯彻落实《生态环境保护督察工作条例》,充分发挥生态环境保护督察利剑作用
  • 坐标大零号湾科创策源区,上海瑞金医院闵行院区正式启动建设
  • 打造信息消费新场景、新体验,上海信息消费节开幕
  • 试点首发进口消费品检验便利化措施,上海海关与上海商务委发文