使用 Java 构建插件系统的最佳方法

2023-09-25Java开发问题
4

本文介绍了使用 Java 构建插件系统的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

您将如何为您的 Java 应用程序实现插件系统?

How would you implement a Plugin-system for your Java application?

是否有可能拥有一个易于使用(对于开发人员)的系统,它可以实现以下目标:

Is it possible to have an easy to use (for the developer) system which achieves the following:

  • 用户将他们的插件放入应用的子目录中
  • 插件可以提供配置屏幕
  • 如果您使用框架,许可证是否与商业开发兼容?

推荐答案

首先你需要一个所有插件都需要实现的接口,例如

First you need an interface that all plugins need to implement, e.g.

public interface Plugin {
    public void load(PluginConfiguration pluginConfiguration);
    public void run();
    public void unload();
    public JComponent getConfigurationPage();
}

插件作者应该将他们的插件捆绑到 JAR 文件中.您的应用程序打开 JAR 文件,然后可以使用 JAR 清单中的属性或 JAR 文件中所有文件的列表来查找实现插件接口的类.实例化那个类,插件就可以使用了.

Plugin authors should then bundle their plugins into JAR files. Your applications opens the JAR file and could then use an attribute from JAR manifest or the list of all files in the JAR file to find the class that implements your Plugin interface. Instantiate that class, the plugin is ready to go.

当然,您可能还想实现某种沙盒,以限制插件能做什么和不能做什么.我创建了一个小型 测试应用程序(和 blogged about it) 由两个插件组成,其中一个被拒绝访问本地资源.

Of course you may also want to implement some kind of sandboxing so that the plugin is restricted in what it can and can not do. I have created a small test application (and blogged about it) that consists of two plugins, one of which is denied access to local resources.

这篇关于使用 Java 构建插件系统的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13