我有三个模块:模块-a、模块-b、模块-c。模块-a 和模块-b 在引导层。我自己创建的模块-c 层。
Module-a 有一个接口com.mod-a.Service
,在它的 module-info 中我有:
module module-a {
exports com.mod-a;
}
Module-c 实现com.mod-a.Service
并在其模块信息中我有:
module module-c {
requires module-a;
provides com.mod-a.Service with com.mod-c.ServiceImpl;
}
module-b 使用 module-c 创建新层,并调用 module-c 服务。在其模块信息中,我有:
module module-b {
requires module-a;
requires java.management;
requires slf4j.api;
uses com.mod-a.Service;
}
在 module-b 中,我以这种方式使用 module-c 创建新层:
ModuleFinder finder = ModuleFinder.of(moduleCPath);
ModuleLayer parent = ModuleLayer.boot();
Configuration cf = parent.configuration().resolve(finder, ModuleFinder.of(), Set.of("module-c"));
ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer layer = parent.defineModulesWithOneLoader(cf, scl);
//the following line prints "module-c"
layer.modules().stream().map(Module::getName).forEach(System.out::println);
但是,创建层后,我无法在 module-b 中调用 module-c 的服务。以下代码:
Iterable<Service> it = ServiceLoader.load(Service.class);
System.out.println("LINE 1");
for (Service service : it) {
System.out.println("Service was called");
service.doIt();
}
System.out.println("LINE 2");
输出:
LINE 1
LINE 2
我的错误是什么?