基于Maven/Gradle多模块springBoot(spring-boot-dependencies)项目架构,适用中小型项目

核心理念:

  1. 单一职责:每个模块专注一个功能领域,边界清晰(如业务、存储、工具模块分工明确)。
  2. 依赖可控:根项目统一管理依赖版本,子模块单向依赖形成无环图,避免冲突。
  3. 分离关注点:通用功能(如工具类)与业务逻辑分离,通过独立模块复用。
  4. 独立构建:模块可单独编译、测试,支持并行开发与 CI/CD。
  5. 统一管理:根项目统筹构建生命周期,支持批量构建与增量优化。

核心目标:通过模块化拆分实现 "高内聚、低耦合",提升大型项目的可维护性与协作效率。

项目整体结构

scss 复制代码
parent-project (父工程,pom类型)
├── common-core (通用核心模块)
├── task-scheduler (任务调度模块)
├── oss-storage (对象存储模块)
├── business-system (业务系统模块)
└── web-controller (Web控制层)

详细目录结构

1. 父工程 (parent-project)

text

复制代码
pom.xml
2. 通用核心模块 (common-core)

text

bash 复制代码
src/
├── main/
│   ├── java/
│   │   └── com/
│   │       └── example/
│   │           └── common/
│   │               ├── annotation/      # 自定义注解
│   │               ├── config/          # 通用配置类
│   │               ├── constant/        # 全局常量
│   │               ├── domain/          # 通用领域模型
│   │               ├── enums/           # 枚举类
│   │               ├── exception/       # 异常处理
│   │               │   ├── GlobalExceptionHandler.java
│   │               │   └── BusinessException.java
│   │               ├── utils/           # 工具类
│   │               │   ├── DateUtils.java
│   │               │   └── StringUtils.java
│   │               └── web/             # Web通用组件
│   │                   ├── AjaxResult.java
│   │                   └── BaseController.java
│   └── resources/
│       └── META-INF/
│           └── spring.factories         # 自动配置声明
pom.xml
3. 任务调度模块 (task-scheduler)

text

bash 复制代码
src/
├── main/
│   ├── java/
│   │   └── com/
│   │       └── example/
│   │           └── task/
│   │               ├── config/           # 任务配置
│   │               │   └── SchedulerConfig.java
│   │               ├── job/              # 任务实现
│   │               │   ├── EmailJob.java
│   │               │   └── ReportJob.java
│   │               ├── service/          # 任务服务
│   │               └── TaskApplication.java # 独立启动类(可选)
│   └── resources/
│       └── application-scheduler.yml     # 任务模块配置
pom.xml
4. OSS存储模块 (oss-storage)

text

bash 复制代码
src/
├── main/
│   ├── java/
│   │   └── com/
│   │       └── example/
│   │           └── oss/
│   │               ├── config/           # OSS配置
│   │               │   └── OssConfig.java
│   │               ├── core/             # 存储核心
│   │               │   ├── OssTemplate.java
│   │               │   └── AbstractOssService.java
│   │               ├── enums/            # OSS枚举
│   │               │   └── OssType.java
│   │               ├── factory/          # 存储工厂
│   │               │   └── OssFactory.java
│   │               ├── service/          # 存储服务实现
│   │               │   ├── impl/
│   │               │   │   ├── AliyunOssServiceImpl.java
│   │               │   │   └── LocalOssServiceImpl.java
│   │               │   └── OssService.java
│   │               └── model/            # OSS模型
│   │                   └── OssFile.java
│   └── resources/
│       └── application-oss.yml          # OSS配置
pom.xml
5. 业务系统模块 (business-system)

text

bash 复制代码
src/
├── main/
│   ├── java/
│   │   └── com/
│   │       └── example/
│   │           └── business/
│   │               ├── domain/           # 领域模型
│   │               │   ├── entity/       # 数据库实体
│   │               │   ├── dto/          # 数据传输对象
│   │               │   └── vo/           # 视图对象
│   │               ├── mapper/           # 数据访问层
│   │               ├── repository/       # JPA存储库
│   │               ├── service/          # 业务服务
│   │               │   ├── impl/         # 服务实现
│   │               │   └── UserService.java
│   │               └── BusinessApplication.java # 业务启动类(可选)
│   └── resources/
│       ├── mapper/                      # MyBatis映射文件
│       └── application-business.yml     # 业务配置
pom.xml
6. Web控制层模块 (web-controller)

text

csharp 复制代码
src/
├── main/
│   ├── java/
│   │   └── com/
│   │       └── example/
│   │           └── web/
│   │               ├── config/           # Web配置
│   │               │   ├── SecurityConfig.java
│   │               │   └── WebMvcConfig.java
│   │               ├── controller/       # 控制器
│   │               │   ├── auth/         # 认证控制器
│   │               │   ├── system/       # 系统控制器
│   │               │   └── OssController.java
│   │               ├── filter/           # 过滤器
│   │               ├── interceptor/      # 拦截器
│   │               └── Application.java  # 主启动类
│   └── resources/
│       ├── static/                      # 静态资源
│       ├── templates/                   # 模板文件
│       └── application.yml              # 主配置文件
pom.xml

`

关键设计说明

  1. 模块依赖关系
  1. 配置管理

    • 每个模块有独立配置文件(application-*.yml)

    • 主模块通过spring.profiles.include整合配置:

      yaml

      yaml 复制代码
      spring:
        profiles:
          include: 
            - business
            - oss
            - scheduler
      1. 启动机制
    • 仅在web-controller模块中设置主启动类

    • 使用组件扫描覆盖所有模块:

      java

      kotlin 复制代码
      @SpringBootApplication(scanBasePackages = {
          "com.example.common",
          "com.example.business",
          "com.example.oss",
          "com.example.task",
          "com.example.web"
      })
  2. 接口定义规范

    • 服务接口定义在API模块(如:business-system)
    • 实现在同模块或单独impl模块
    • 控制层只调用服务接口
  3. 跨模块调用示例

    java

    less 复制代码
    // 在web-controller中调用OSS服务
    @RestController
    public class FileController {
        @Autowired
        private OssService ossService;
        
        @PostMapping("/upload")
        public AjaxResult upload(@RequestParam MultipartFile file) {
            OssFile ossFile = ossService.upload(file);
            return AjaxResult.success(ossFile);
        }
    }
  4. 打包部署

    • 父工程执行:mvn clean install
    • 单独打包web模块:mvn package -pl web-controller -am

最佳实践建议

  1. 模块通信

    • 使用DTO对象进行跨模块数据传输
    • 通过Feign Client实现微服务间通信(可选)
  2. 安全控制

    • 在web-controller模块统一处理安全认证
    • 使用Spring Security + JWT
  3. API文档

    • 在common-core集成Swagger配置
    • 在web-controller启用文档生成
  4. 日志管理

    • 在common-core配置全局日志框架
    • 使用MDC实现请求链路追踪
  5. 多环境支持

    bash

    ini 复制代码
    # 启动命令指定环境
    java -jar web-controller.jar --spring.profiles.active=prod

此架构设计具有以下优势:

  • 模块化开发:各团队可并行开发不同模块
  • 职责分离:业务逻辑与技术实现解耦
  • 易于扩展:新增功能只需添加新模块
  • 复用性强:通用组件统一管理
  • 部署灵活:可独立部署任务调度等模块

以下是完整的父子模块依赖关系结构,包含每个模块的详细POM配置:

1. 父工程 (parent-project/pom.xml)

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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>Parent Project</name>
    <description>Spring Boot多模块父工程</description>
    
    <modules>
        <module>common-core</module>
        <module>task-scheduler</module>
        <module>oss-storage</module>
        <module>business-system</module>
        <module>web-controller</module>
    </modules>

    <!-- 统一属性管理 -->
    <properties>
        <java.version>17</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring-boot.version>3.1.0</spring-boot.version>
        <lombok.version>1.18.28</lombok.version>
    </properties>

    <!-- 依赖版本管理 -->
    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot依赖管理 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            
            <!-- 通用依赖 -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <scope>provided</scope>
            </dependency>
            
            <!-- 阿里云OSS -->
            <dependency>
                <groupId>com.aliyun.oss</groupId>
                <artifactId>aliyun-sdk-oss</artifactId>
                <version>3.16.1</version>
            </dependency>
            
            <!-- XXL-JOB -->
            <dependency>
                <groupId>com.xuxueli</groupId>
                <artifactId>xxl-job-core</artifactId>
                <version>2.4.0</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- 构建插件管理 -->
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring-boot.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.11.0</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <encoding>${project.build.sourceEncoding}</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

2. 通用核心模块 (common-core/pom.xml)

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">
    <parent>
        <artifactId>parent-project</artifactId>
        <groupId>com.example</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>common-core</artifactId>
    <name>Common Core Module</name>
    <description>通用核心模块</description>
    
    <dependencies>
        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        
        <!-- Apache Commons -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        
        <!-- Jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>
</project>

3. 任务调度模块 (task-scheduler/pom.xml)

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">
    <parent>
        <artifactId>parent-project</artifactId>
        <groupId>com.example</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>task-scheduler</artifactId>
    <name>Task Scheduler Module</name>
    <description>任务调度模块</description>
    
    <dependencies>
        <!-- Spring Boot Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        
        <!-- 任务调度 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>
        
        <!-- XXL-JOB -->
        <dependency>
            <groupId>com.xuxueli</groupId>
            <artifactId>xxl-job-core</artifactId>
        </dependency>
        
        <!-- 依赖通用模块 -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common-core</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</project>

4. OSS存储模块 (oss-storage/pom.xml)

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">
    <parent>
        <artifactId>parent-project</artifactId>
        <groupId>com.example</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>oss-storage</artifactId>
    <name>OSS Storage Module</name>
    <description>对象存储模块</description>
    
    <dependencies>
        <!-- Web支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- 阿里云OSS -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
        </dependency>
        
        <!-- 文件处理 -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
        
        <!-- 依赖通用模块 -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common-core</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</project>

5. 业务系统模块 (business-system/pom.xml)

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">
    <parent>
        <artifactId>parent-project</artifactId>
        <groupId>com.example</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>business-system</artifactId>
    <name>Business System Module</name>
    <description>业务系统模块</description>
    
    <dependencies>
        <!-- 数据访问 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        
        <!-- MySQL驱动 -->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>
        
        <!-- MyBatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.2</version>
        </dependency>
        
        <!-- 依赖其他模块 -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common-core</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>oss-storage</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</project>

6. Web控制层模块 (web-controller/pom.xml)

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">
    <parent>
        <artifactId>parent-project</artifactId>
        <groupId>com.example</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>web-controller</artifactId>
    <name>Web Controller Module</name>
    <description>Web控制层模块</description>
    
    <!-- 打包为可执行JAR -->
    <packaging>jar</packaging>
    
    <dependencies>
        <!-- Spring Boot Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- Spring Boot Actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        
        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        
        <!-- 验证框架 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        
        <!-- 依赖其他业务模块 -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>business-system</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>task-scheduler</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>oss-storage</artifactId>
            <version>${project.version}</version>
        </dependency>
        
        <!-- 开发工具 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Spring Boot Maven插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.example.web.Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

依赖关系图

graph TD A[web-controller] --> B[business-system] A --> C[oss-storage] A --> D[task-scheduler] B --> E[common-core] C --> E D --> E B --> C

关键设计要点

  1. 依赖继承机制

    • 所有子模块继承父POM的dependencyManagement配置
    • 子模块只需声明依赖,无需指定版本号
    • 父POM统一管理第三方依赖版本
  2. 模块隔离原则

    • 每个模块只声明必要的依赖
    • 业务模块不依赖Web相关组件
    • 通用模块保持最小依赖集
  3. 构建优化

    • 使用<optional>true</optional>标记开发工具依赖
    • 父POM集中管理插件版本
    • web-controller模块配置Spring Boot打包插件
  4. 多环境支持: 在父POM中添加profile支持:

    xml 复制代码
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <spring.profiles.active>prod</spring.profiles.active>
            </properties>
        </profile>
    </profiles>
  5. 启动类配置: 在web-controller模块中:

    java 复制代码
    @SpringBootApplication(scanBasePackages = {
        "com.example.common",
        "com.example.business",
        "com.example.oss",
        "com.example.task",
        "com.example.web"
    })
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
  6. 依赖范围控制

    • Lombok使用provided范围
    • 测试依赖统一在父POM管理
    • 可选依赖标记为optional

这种结构提供了清晰的模块边界和依赖管理,支持团队并行开发,同时确保构建的一致性和可维护性。

相关推荐
CZZDg6 分钟前
Redis Sentinel哨兵集群
java·网络·数据库
石头wang6 分钟前
intellij idea的重命名shift+f6不生效(快捷键被微软输入法占用)
java·ide·intellij-idea
止水编程 water_proof7 分钟前
java堆的创建与基础代码解析(图文)
java·开发语言
zhougl99611 分钟前
git项目,有idea文件夹,怎么去掉
java·git·intellij-idea
相与还22 分钟前
IDEA实现纯java项目并打包jar(不使用Maven,Spring)
java·intellij-idea·jar
程序无bug27 分钟前
后端3行代码写出8个接口!
java·后端
摇滚侠29 分钟前
idea删除的文件怎么找回
java·ide·intellij-idea
菜鸟的迷茫30 分钟前
Spring Boot 3 + Spring Security 6 + JWT 打造企业级权限系统(拿走即用)
java
笑衬人心。1 小时前
Spring的`@Value`注解使用详细说明
java·后端·spring
hac13221 小时前
Spring Boot 双数据源配置
java·spring boot·后端