如何创建可插入的 Java 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a pluginable Java program?
提问by pek
I want to create a Java program that can be extended with plugins. How can I do that and where should I look for?
我想创建一个可以用插件扩展的 Java 程序。我该怎么做,我应该在哪里寻找?
I have a set of interfaces that the plugin must implement, and it should be in a jar. The program should watch for new jars in a relative (to the program) folder and registered them somehow.
我有一组插件必须实现的接口,它应该在一个 jar 中。程序应该在相对(程序)文件夹中观察新的 jars 并以某种方式注册它们。
Although I do like Eclipse RCP, I think it's too much for my simple needs.
虽然我确实喜欢 Eclipse RCP,但我认为它对于我的简单需求来说太过分了。
Same thing goes for Spring, but since I was going to look at it anyway, I might as well try it.
Spring 也是一样,但既然我要看看它,我不妨试试。
But still, I'd prefer to find a way to create my own plugin "framework" as simple as possible.
但是,我还是更愿意找到一种方法来尽可能简单地创建我自己的插件“框架”。
采纳答案by Steve M
I've done this for software I've written in the past, it's very handy. I did it by first creating an Interface that all my 'plugin' classes needed to implement. I then used the Java ClassLoaderto load those classes and create instances of them.
我已经为我过去编写的软件完成了这项工作,它非常方便。我首先创建了一个接口,我的所有“插件”类都需要实现该接口。然后我使用 Java ClassLoader加载这些类并创建它们的实例。
One way you can go about it is this:
你可以这样做的一种方法是:
File dir = new File("put path to classes you want to load here");
URL loadPath = dir.toURI().toURL();
URL[] classUrl = new URL[]{loadPath};
ClassLoader cl = new URLClassLoader(classUrl);
Class loadedClass = cl.loadClass("classname"); // must be in package.class name format
That has loaded the class, now you need to create an instance of it, assuming the interface name is MyModule:
这已经加载了类,现在您需要创建它的一个实例,假设接口名称是 MyModule:
MyModule modInstance = (MyModule)loadedClass.newInstance();
回答by John with waffle
Have you considered building on top of Eclipse's Rich Client Platform, and then exposing the Eclipse extension framework?
您是否考虑过在 Eclipse 的富客户端平台之上构建,然后公开 Eclipse 扩展框架?
Also, depending on your needs, the Spring Framework might help with that and other things you might want to do: http://www.springframework.org/
此外,根据您的需要,Spring Framework 可能会帮助您解决这个问题以及您可能想做的其他事情:http: //www.springframework.org/
回答by jsight
I recommend that you take a close look at the Java Service Provider (SPI) API. It provides a simple system for finding all of the classes in all Jars on the classpath that expose themselves as implementing a particular service. I've used it in the past with plugin systems with great success.
我建议您仔细查看Java Service Provider (SPI) API。它提供了一个简单的系统,用于查找类路径上所有 Jars 中的所有类,这些类将自己暴露为实现特定服务。我过去曾将它与插件系统一起使用并取得了巨大成功。
回答by David Koelle
Look into OSGi.
查看OSGi。
On one hand, OSGi provides all sorts of infrastructure for managing, starting, and doing lots of other things with modular software components. On the other hand, it could be too heavy-weight for your needs.
一方面,OSGi 提供了各种基础设施,用于管理、启动和使用模块化软件组件执行许多其他操作。另一方面,它可能对您的需求来说太重了。
Incidentally, Eclipse uses OSGi to manage its plugins.
顺便说一下,Eclipse 使用 OSGi 来管理它的插件。
回答by Toni Menzel
At the home-grown classloader approach: While its definitely a good way to learn about classloaders there is something called "classloader hell", mostly known by people who wrestled with it when it comes to use in bigger projects. Conflicting classes are easy to introduce and hard to solve.
在本土类加载器方法中:虽然它绝对是了解类加载器的好方法,但有一种叫做“类加载器地狱”的东西,大多数人都知道在更大的项目中使用它时与它搏斗的人。冲突的类很容易引入,但很难解决。
And there is a good reason why eclipse made the move to OSGi years ago. So, if its more then a pet project, take a serious look into OSGi. Its worth looking at. You'll learn about classloaders PLUS an emerging technolgy standard.
Eclipse 多年前转向 OSGi 是有充分理由的。因此,如果它不仅仅是一个宠物项目,请认真研究 OSGi。值得一看。您将了解类加载器以及一种新兴的技术标准。
回答by Steen
Although I'll second the accepted solution, if a basic plugin support is needed (which is the case most of the time), there is also the Java Plugin Framework(JPF) which, though lacking proper documentation, is a very neat plugin framework implementation.
虽然我会支持已接受的解决方案,但如果需要基本的插件支持(大多数情况下都是这种情况),还有Java Plugin Framework(JPF),虽然缺乏适当的文档,但它是一个非常简洁的插件框架执行。
It's easily deployable and - when you get through the classloading idiosynchrasies - very easy to develop with. A comment to the above is to be aware that plugin loadpaths below the plugin directory must be named after the fullclasspath in addition to having its class files deployed in a normal package path named path. E.g.
它易于部署,并且 - 当您了解类加载特性时 - 非常易于开发。对上面的注释是要注意,除了将其类文件部署在名为 path 的普通包路径中之外,插件目录下的插件加载路径还必须以完整的类路径命名。例如
plugins
`-com.my.package.plugins
`-com
`-my
`-package
`-plugins
|- Class1.class
`- Class2.class