如何在java中编写自己的插件加载器? [英] How can I write my own plugin loader in java?

查看:128
本文介绍了如何在java中编写自己的插件加载器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的java程序中实现插件工具?

How can I implement a plugin facility in my java program?

我正在使用Java。我目前的项目是与通用电子硬件有关的东西,它有一个自定义命令集。

I am working with Java. My current project is something related to a general purpose electronics hardware, which have a custom command set.

现在有一个通用的GUI可以访问硬件。硬件在不同环境中以不同方式运行,即针对不同客户端。现在的问题是GUI必须能够添加插件。插件意味着,它必须能够为拥有该特权的客户提供特定设施。从客户方面来说,添加插件很简单,只需单击按钮即可添加特定工具。

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.

推荐答案

您需要提供以下内容:


  • 创建一个API,您的插件可用于更改/扩展程序的行为(恕我直言,这是最棘手的部分)

  • 定义插件的公共条目,例如,特定于插件的属性文件,用于定义插件的入口点或扩展点(实现接口的插件类的类名) )

  • 从您选择的位置动态加载插件,例如,来自特定目录的所有* .jar文件(请查看 URLClassLoader

  • create an API which your plug-ins can use to change/extend the behavior of your program (IMHO this is the trickiest part)
  • define a common entry to the plug-ins, e.g., a plug-in-specific properties file defining the entry point or extension points of your plug-in (class name of a plug-in class implementing an interface)
  • dynamically load the plug-ins from a location of your choice, e.g., all *.jar files from a specific directory (take a look at URLClassLoader)

API建议:


  • 更喜欢接口(摘要)类

  • 帮助用户快速查看她可以实现哪些接口(例如, IAction ,注意前导 I )以及您的应用程序提供的插件使用情况(例如 WindowManager

  • prefer interfaces over (abstract) classes
  • it might be useful to help the user to quickly see which interfaces she could implement (e.g. IAction, notice the leading I) and which are provided by your application for plug-in usage (e.g. WindowManager)

这篇关于如何在java中编写自己的插件加载器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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