簡體   English   中英

ServiceLoader 找不到我的實現

[英]ServiceLoader can't find my implementation

我正在嘗試將 ServiceLoader 與模塊系統一起使用,與此處文檔中將服務提供程序部署為模塊header 中所示的方式相同 - 單擊

我有以下項目:

模塊 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;
}

模塊 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 提供 Window

事實證明,問題是特定於 IDE 的。 由於我的 IDE (Intellij IDEA) 有自己的附加模塊系統,我需要添加tester.gui模塊作為tester.client模塊的依賴項。

@samabcde提供的解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM