如何用java编写自己的插件加载器?
如何在我的 java 程序中实现插件功能?
我正在使用 Java。我当前的项目与通用电子硬件相关,它具有自定义命令集。
现在有一个通用的 GUI,通过它可以访问硬件。硬件在不同的环境中(即针对不同的客户端)以不同的方式运行。现在的问题是 GUI 必须能够添加插件。插件意味着,它必须能够向拥有特权的客户提供特定的便利。从客户端来看,插件的添加很简单,只需点击一个按钮即可添加特定设施。
我之所以考虑插件是因为只有在交付核心产品之后才会引入越来越多的设施。
How can I implement a plugin facility in my java program?
I am working with Java. My current project is something related to a general purpose electronics hardware, which have a custom command set.
Now there is a general GUI through which one can access the hardware. The hardware behaves in different ways in different environment, i.e. for different clients. Now the problem is that the GUI must be capable of adding plugins. Plugin means, it must be capable of giving a particular facility to a customer who has the privilege. From the customer side, the addition of plugin is to be simple, like just click on a button to add a particular facility.
The reason why I think about plugins is that more and more facility will be introduced only after delivering the core product.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要提供以下内容:
URLClassLoader
)API 建议:
IAction
,注意前面的 < code>I),并由您的应用程序提供供插件使用(例如WindowManager
)You need to provide following things:
URLClassLoader
)API suggestions:
IAction
, notice the leadingI
) and which are provided by your application for plug-in usage (e.g.WindowManager
)在任何面向对象的语言中实现插件背后的主要思想是定义插件和相关类必须实现的一组通用接口,然后通过反射加载和实例化它们......
您可以使用抽象工厂模式,以便任何对象插件需要的可以被实例化...
假设你的插件架构只有3个接口,每个插件必须提供实现这些接口的类,那么你的插件架构可以是这样的:
然后让插件在XML中定义文件或属性文件插件工厂的类名插件:
例如,假设您的插件定义:
然后在属性文件中定义
// 插件的plugin.jar中提供的文件plugin.properties
plugin.factory.class = com.my.plugin.PluginFactoryImpl;
在您的应用程序中可以执行
// 这里根据您的喜好调用创建的类。
谢谢
巴勃罗
The main idea behind implementing plugins in any object oriented language, is to define a set of common interfaces that the plugin and related classes must implement, and then load and instantiate them via reflection...
You can use abstract factory pattern so that any objects needed by the plugin can be instantiated...
Let's say that your plugin architecture has only 3 interfaces and each plugin must provide classes that implement those interfaces, then your plugin architecture could be like this:
And then let the plugins to define in an XML file or properties file the class name of the plugin factory for the plugin:
For instance let's say your plugin defines:
And then define in the properties file
// File plugin.properties provided in the plugin.jar of the plugin
plugin.factory.class = com.my.plugin.PluginFactoryImpl;
In your application can do
// Here invoke created classes as you like.
Thanks
Pablo
您可以随时向应用程序添加 jar 或插件。您无需执行任何特殊操作即可实现此目的。
如果您使用 OGSi,您可以更轻松地管理此操作,支持同一 jar 的多个版本并在应用程序运行时删除它们。我建议看看 Apache Karaf + iPOJO
You can add a jar or plugin to an application at anytime. You don't have to do anything special to achieve this.
If you use OGSi you can manage this easier, support multiple version of the same jar and remove them while the application is running. I suggest looking at Apache Karaf + iPOJO
JAR 文件格式有自己的小系统用于管理插件,它用于 Java SE 的多个部分,包括 JDBC 驱动程序管理。只需定义一个服务接口,将带有实现的 JAR 文件放在类路径上,然后使用
ServiceLoader.load
。The JAR file format has its own little system for managing plugins, which is used for several parts of Java SE, including JDBC driver management. Just define a service interface, put JAR files with implementations on the classpath and load the implementations with
ServiceLoader.load
.