使用Java构建插件系统的最佳方式 [英] Best way to build a Plugin system with Java

查看:126
本文介绍了使用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界面的类。实例化该类,插件已经准备好了。

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.

当然,您可能还需要实现某种沙盒,以便插件受限于可以和可以不做。我创建了一个小型测试应用程序(和关于它),包含两个插件,其中一个插件被拒绝访问本地资源。

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构建插件系统的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆