Java SPI - ServiceLoader - 将多个JAR添加到APP类路径 [英] Java SPI - ServiceLoader - adding multiple JAR's to APP classpath

查看:426
本文介绍了Java SPI - ServiceLoader - 将多个JAR添加到APP类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中实现此逻辑时遇到问题。我使用ServiceLoader,我不知道,或者它是否可能,但我想动态地将JAR添加到app的某个已知目录,因此应用程序可以在启动时加载它们。我唯一获得的是我知道如何创建可扩展的ONE JAR(使用META-INF / services),因此我可以为此JAR添加越来越多的提供者和类(实现)。 JAR包含在类路径中,就像普通的库一样,所以app知道它。我想要实现的目标:
我不想扩展一个jar文件,但我希望通过添加更多jars(如插件与提供程序(服务)来扩展应用程序,但我不知道如何。

I have a problem implementing this logic in my project. Im using ServiceLoader and I don't know or if it's even possible, but I want to dynamically add JARs to some known directory for app, so the app can load them when it starts. The only thing I achieved was that I know how to create extensible ONE JAR (with META-INF/services), so I can add more and more providers and classes (implementations) to this JAR. The JAR is included to classpath like ordinary library, so app knows about it. What I want to achieve: I don't want to extend one jar file, but I want to extend app by adding more jars like plugins with providers (services), but I don't know how.

感谢您的帮助!

推荐答案

您只需要(a)URLClassLoader即可使用这些目录中所有JAR文件的URL构造,并且(b)调用 ServiceLoader.load(Class service,ClassLoader loader)

You just need (a) a URLClassLoader that is constructed with the URLs of all the JAR files in those directories, and (b) call ServiceLoader.load(Class service, ClassLoader loader).

这篇关于Java SPI - ServiceLoader - 将多个JAR添加到APP类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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