如何解决ServiceLoader 找不到我的实现
我正在尝试将 ServiceLoader 与模块系统一起使用,与此处文档中的 将服务提供者部署为模块 标头中所示的方式相同 - {{3} }
我有以下项目:
模块 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
}
}
模块信息.java
import tester.common.Showable;
module tester.client {
requires tester.common;
uses Showable;
}
module tester.common
package tester.common;
public interface Showable {
void show();
}
模块信息.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() {
}
}
模块信息.java
module tester.gui {
requires tester.common;
provides tester.common.Showable with tester.gui.Window;
}
问题:
ServiceLoader 不加载我的实现。
- tester.client 使用 Showable
- tester.common 导出 Showable
- tester.gui 提供 Showable with Window
解决方法
事实证明问题是特定于 IDE 的。由于我的 IDE (Intellij IDEA) 有自己的附加模块系统,我需要添加 tester.gui
模块作为 tester.client
模块的依赖项。
@samabcde 提供的解决方案
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。