You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于IntelliJ IDEA模块依赖排序功能的验证疑问及示例需求

模块依赖排序的验证方案及意义解释

为什么你的测试没体现出依赖顺序的影响

你的测试案例中,所有类的全限定名都是唯一的:TestModule2的A、TestModule3的B、TestModule1的MyUtils彼此没有类名和包名冲突。这种情况下,类加载器会在所有依赖模块中定位到唯一的目标类,不管依赖顺序如何都能正常运行,自然看不出差异。

依赖排序的核心意义

当多个依赖模块中存在全限定名完全相同的类时,编译期和运行期会按照依赖列表的顺序优先选用排在前面的模块中的类,排序直接决定了程序会加载哪个版本的类,进而影响运行结果。

可验证的示例项目

模块结构

创建3个模块:ModuleA(主模块)、ModuleB、ModuleC,其中ModuleB和ModuleC包含同包同名的类,ModuleA依赖这两个模块。

代码实现

ModuleB 中的 com.test.Demo

package com.test;

public class Demo {
    public static String getMessage() {
        return "来自ModuleB的消息";
    }
}

ModuleC 中的 com.test.Demo

package com.test;

public class Demo {
    public static String getMessage() {
        return "来自ModuleC的消息";
    }
}

ModuleA 中的主类 Main

import com.test.Demo;

public class Main {
    public static void main(String[] args) {
        System.out.println(Demo.getMessage());
    }
}

验证步骤

  1. 打开IntelliJ IDEA的项目结构设置(File → Project Structure → Modules → ModuleA → Dependencies)
  2. 将ModuleB排在ModuleC前面,运行Main类,输出:来自ModuleB的消息
  3. 调整依赖顺序,把ModuleC移到ModuleB前面,再次运行Main类,输出:来自ModuleC的消息

这个案例清晰体现了依赖顺序的作用:排在前面的模块中的同名类会被优先加载,直接改变程序输出结果。

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 21:16:14