问题描述
您将如何为Java应用程序实现插件系统?
是否可以使用易于使用(对于开发人员)系统,以实现以下操作:
- 用户将其插件放入应用程序的子目录
- 插件可以提供配置屏幕
- 如果您使用框架,许可证是否与商业开发兼容?
推荐答案
首先,您需要所有插件需要实现的接口,例如
public interface Plugin { public void load(PluginConfiguration pluginConfiguration); public void run(); public void unload(); public JComponent getConfigurationPage(); }
插件作者应将其插件捆绑到JAR文件中.您的应用程序打开JAR文件,然后可以使用JAR清单中的属性或JAR文件中所有文件的列表来查找实现您的插件接口的类.实例化该类,插件已准备就绪.
当然,您可能还需要实现某种沙箱,以便插件受到限制,无法做到.我创建了一个小 test应用程序/blog.pterodactylus.net/2013/07/19/sandboxing/" rel =" noreferrer">关于它的博客)由两个插件组成,其中一个被拒绝访问本地资源.
其他推荐答案
使用 osgi .
它是Eclipse插件系统的基础. Equinox 是Eclipse的实现(许可EPL)和 felix 是Apache项目的实现(许可的Apache公共许可证).
Eclipse提供了一个具体示例,OSGI可以涵盖您提到的点(或者您可以在 Eclipse RCP 如果您想要完整的Eclipse/swt/jface堆栈).
其他推荐答案
从1.6开始,就有 java. util.serviceloader 如果要编码自己的简单系统,可以使用.
但是,如果您想要的是基本功能,请使用现有框架之一.
问题描述
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:
- Users put their plugins into a subdirectory of the app
- The Plugin can provide a configuration screen
- If you use a framework, is the license compatible with commercial developement?
推荐答案
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(); }
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.
其他推荐答案
Use OSGi.
It is the foundation of the Eclipse plug-in system. Equinox is Eclipse's implementation (licensed EPL) and Felix is the Apache Project's implementation (licensed Apache Public License).
Eclipse provides a concrete example that OSGi can cover the points you mentioned (or you could just build your application on top of Eclipse RCP if you want a full Eclipse/SWT/JFace stack).
其他推荐答案
Since 1.6, there's been java.util.ServiceLoader which can be used if you want to code your own simple system.
But if you want anything more than basic features, use one of the existing frameworks.