You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

多项目应用搭建与Spring Modulith配置:跨包合并User模块

跨模块合并Spring Modulith模块的实现方案

可行性结论

完全可行。Spring Modulith支持通过显式指定模块归属自定义模块命名策略,将分散在不同根包下的类合并为同一个逻辑模块,刚好能满足你把com.shared.usercom.a.user合并为单一User模块的需求。


配置步骤

假设你的Gradle项目中,Application A已正确依赖Common领域库(在A的build.gradle中添加implementation project(':common')),以下是两种具体配置方式:

方式一:显式标注模块归属(简单直接)

直接给两个包下的核心类添加@Module注解,指定统一的模块名称:

  • com.shared.user包的类(如User实体、UserRepository)上添加:
    @Module("user")
    public class User { /* ... */ }
    
  • com.a.user包的类(如UserServiceUserController)上添加:
    @Module("user")
    public class UserService { /* ... */ }
    

Spring Modulith会自动将所有标注了@Module("user")的类归为同一个user模块,忽略它们的根包差异。

方式二:自定义模块命名策略(适合批量处理)

如果两个包下的类较多,不想逐个标注,可以自定义命名策略,让匹配规则自动识别模块:

  1. 创建命名策略实现类:
    import org.springframework.modulith.core.ModuleNamingStrategy;
    import org.springframework.modulith.core.ProjectModule;
    
    public class UserModuleNamingStrategy implements ModuleNamingStrategy {
        @Override
        public String getModuleName(ProjectModule module) {
            String basePackage = module.getBasePackage().getName();
            // 匹配所有以".user"结尾的子包,归为user模块
            if (basePackage.endsWith(".user")) {
                return "user";
            }
            // 其他包沿用默认命名规则
            return ModuleNamingStrategy.DEFAULT.getModuleName(module);
        }
    }
    
  2. 在Application A的配置类中注册该策略:
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.modulith.core.ModuleNamingStrategy;
    
    @Configuration
    public class ModulithConfig {
        @Bean
        public ModuleNamingStrategy moduleNamingStrategy() {
            return new UserModuleNamingStrategy();
        }
    }
    

验证配置生效

可以通过Spring Modulith的测试工具验证模块结构是否符合预期:

  1. 添加测试依赖到Application A的build.gradle
    testImplementation 'org.springframework.modulith:spring-modulith-test'
    
  2. 编写验证测试:
    import org.junit.jupiter.api.Test;
    import org.springframework.modulith.test.SpringModulithTest;
    
    @SpringModulithTest
    class ModulithStructureValidationTest {
    
        @Test
        void verifyModuleComposition() {
            // 自动生成模块结构报告,验证user模块包含两个包的类
            org.springframework.modulith.test.ApplicationModuleTest.verifyModules();
        }
    }
    

运行测试后,控制台会输出模块结构详情,你可以看到user模块同时包含com.shared.usercom.a.user下的所有类。


内容的提问来源于stack exchange,提问作者Rolan

火山引擎 最新活动