如何自定义 ServiceLoader 查找插件 Jar 的位置

How to customize where ServiceLoader looks for Plugin Jars

我有一个包含两个项目的多模块项目:CoreA。这个想法是启动/ 运行 A 每当 Core 启动时。

如何自定义 ServiceLoader 以从核心查找和调用 Plugins 文件夹中的模块?

plugin-Project
    + Core
        + src\main\java
            Core.java

    + A
        + src\main\java
            A.java

    + Plugins

核心

public class Core extends Application {

    private ServiceLoader<View> views;
    private BorderPane mainBorderPane;

    @Override
    public void init() {
        loadViews();
    }

    private void loadViews() {
        views = ServiceLoader.load(View.class);
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Ui Application");

        mainBorderPane = new BorderPane();
        mainBorderPane.setTop(createMenuBar());

        Scene scene = new Scene(new Group(), 800, 605);
        scene.setRoot(mainBorderPane);

        stage.setScene(scene);
        stage.show();
    }

    private MenuBar createMenuBar() {
        MenuBar menuBar = new MenuBar();
        Menu viewMenu = new Menu("Views");
        menuBar.getMenus().add(viewMenu);

        ToggleGroup toggleGroup = new ToggleGroup();

        views.forEach(v -> {
            RadioMenuItem item = new RadioMenuItem(v.getName());
            item.setToggleGroup(toggleGroup);
            item.setOnAction(event -> {
                Label label = new Label(v.getName());
                mainBorderPane.setLeft(label);
                mainBorderPane.setCenter(v.getView());
            });
            viewMenu.getItems().add(item);
        });
        return menuBar;
    }

    public static void main(String[] args) {
        launch(args);
    }

}

View.java

public interface View {

    String getName();

    Node getView();
}

场景

我正在开发的应用程序是一个多模块独立桌面应用程序。例如,Core 将在左侧放置一个窗格 (left-pane)。左窗格将从任何实现名为 LeftPane 的接口的模块中接受 nodesA 实现了 LeftPane 接口。每当 Core 启动时,它应该扫描一个文件夹,在这种情况下是插件,并自动启动那里的所有包,包括 A,它将继续填充左窗格。

最简单的方法当然是让插件已经在 class路径上。然后你可以简单地通过 ServiceLoader.

访问接口

或者您提供一种机制,它将在特定位置检测您的插件 jar 文件并将它们添加到 class 路径。这是棘手的部分。一种方法是为您的应用程序使用自定义 ClassLoader,允许将 jar 文件添加到 class 路径。

我选择了一种不同的方法,即访问我的应用程序正在使用的 ClassLoader 的非 public API:

private void addFile(File f) throws IOException // URL to your plugin jar file
{
    addURL(f.toURI().toURL());
}

private void addURL(URL u) throws IOException
{
    URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class sysclass = URLClassLoader.class;

    try {
        Method method = sysclass.getDeclaredMethod("addURL", parameters);
        method.setAccessible(true);
        method.invoke(sysloader, new Object[] {u});
    } catch (Throwable t) {
        t.printStackTrace();
    }

}

当插件 jar 文件位于 class 路径时,您可以通过 ServiceLoader 访问公开的接口。让我用一个例子来说明这一点。公开的界面可能如下所示:

/**
 * Interface to allow plugins to contribute resource reference properties.
 */
public interface IResourcePropertyLoader {
    /**
     * Retrieve the base name for the additional text resource.
     * @return
     */
    String getResourcePropertyBaseName();
}

接口(也可以是基础 class)是核心应用程序的一部分。该插件有一个实现此接口的 class。

接下来查找该接口的所有实现:

ServiceLoader<ITextPropertyLoader> loader = ServiceLoader.load(ITextPropertyLoader.class, ClassLoader.getSystemClassLoader());

ServiceLoader 实现了 Iterable,因此您可以遍历所有实现:

for (ITextPropertyLoader textProperty : loader) {
    final String textPropertyBaseName = textProperty.getTextPropertyBaseName();
    System.out.println("Found text property with name: " + textPropertyBaseName);
}

也看看Oracles documentation on this as well as this question