简体   繁体   English

ServiceLoader 找不到我的实现

[英]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 不加载我的实现。

  • tester.client uses Showable tester.client 使用 Showable
  • tester.common exports Showable tester.common 导出 Showable
  • tester.gui provides Showable with Window tester.gui 为 Showable 提供 Window

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模块的依赖项。

Solution provided by @samabcde @samabcde提供的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM