我试图在模块系统中使用ServiceLoader,就像将服务提供者作为模块头部署在这里的文档- 点击中一样。
我有以下项目:
模块tester.client
package tester.client;
import tester.common.Showable;
import java.util.ServiceLoader;
public class Main {
public static void main(String[] args) {
ServiceLoader<Showable> loader = ServiceLoader.load(Showable.class);
loader.findFirst().orElseThrow(); //throws Exception
}
}module-info.java
import tester.common.Showable;
module tester.client {
requires tester.common;
uses Showable;
}模块tester.common
package tester.common;
public interface Showable {
void show();
}module-info.java
module tester.common {
exports tester.common;
}模块tester.gui
package tester.gui;
import tester.common.Showable;
public class Window implements Showable {
@Override
public void show() {
}
}module-info.java
module tester.gui {
requires tester.common;
provides tester.common.Showable with tester.gui.Window;
}问题:
ServiceLoader不加载我的实现。
发布于 2021-06-07 17:22:49
结果发现问题是特定于IDE的。由于我的IDE (Intellij )有自己的附加模块系统,我需要添加tester.gui模块作为tester.client模块的依赖项。
@samabcde提供的解决方案
https://stackoverflow.com/questions/67872101
复制相似问题