[英]ServiceLoader can't find my implementation
I'm trying to use ServiceLoader with modules system, the same way as shown in the Deploying service providers as modules header in the documentation here - click我正在尝试将 ServiceLoader 与模块系统一起使用,与此处文档中将服务提供程序部署为模块header 中所示的方式相同 - 单击
I have the following project:我有以下项目:
module tester.client模块 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模块信息.java
import tester.common.Showable;
module tester.client {
requires tester.common;
uses Showable;
}
module tester.common模块 tester.common
package tester.common;
public interface Showable {
void show();
}
module-info.java模块信息.java
module tester.common {
exports tester.common;
}
module tester.gui模块 tester.gui
package tester.gui;
import tester.common.Showable;
public class Window implements Showable {
@Override
public void show() {
}
}
module-info.java模块信息.java
module tester.gui {
requires tester.common;
provides tester.common.Showable with tester.gui.Window;
}
THE PROBLEM:问题:
ServiceLoader doesn't load my implementation. ServiceLoader 不加载我的实现。
It turned out the problem is IDE-specific.事实证明,问题是特定于 IDE 的。 As my IDE (Intellij IDEA) has its own additional modules system, I needed to add
tester.gui
module as a dependency of tester.client
module.由于我的 IDE (Intellij IDEA) 有自己的附加模块系统,我需要添加
tester.gui
模块作为tester.client
模块的依赖项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.