在 NetBeans 平台之外使用 Lookup API

发布于 2024-11-10 06:25:34 字数 742 浏览 2 评论 0 原文

我正在尝试评估我们的商店在不使用整个 NetBeans 平台的情况下使用 NetBeans Lookup API 是否合适。

到目前为止,我设法使用此代码创建了一个项目:

 for (SomeInterface si : Lookup.getDefault().lookupAll(SomeInterface.class)) {
     si.doSomething();
 }

我还创建了几个其他项目,每个项目都有一个实现 SomeInterface 的 AnImplementation 类,以及随附的文件 META-INF/services/path.to.SomeInterface 包含引用的行类(例如“other.path.to.AnImplementation”)。

当我将这些实现项目添加到 NetBeans IDE 中主项目的库(依赖项)时,它工作正常,并且我可以从两个实现中看到 doSomething() 的连续结果。

我的问题是如何在不引用主项目中的子项目的情况下使其工作;构建时,子项目的 jar 不会包含在主项目生成的 jar 中,并且可以随意添加或删除它们,从而改变上述代码的结果。

如果我没记错的话,这是 Lookup API 文档中宣传的行为。 提前致谢。

编辑:目前,我的结论是,如果没有 NetBeans 平台(或 OSGi?),就不可能检测启动时存在哪些服务提供商。您需要在类路径中引用它们的 jar,从而在启动之前识别它们。请随意证明我错了。

I'm trying to evaluate whether it's appropriate for our shop to use the NetBeans Lookup API without the whole NetBeans Platform.

So far, I managed to create a project with this code :

 for (SomeInterface si : Lookup.getDefault().lookupAll(SomeInterface.class)) {
     si.doSomething();
 }

I also created a couple of other projects, each with an AnImplementation class implementing SomeInterface, and the accompanying file META-INF/services/path.to.SomeInterface containing a line referencing the class (eg. "other.path.to.AnImplementation").

When I add these implementing projects to the libraries (dependencies) of the main project in the NetBeans IDE, it works fine and I can see the successive results of doSomething() from both implementations.

My question is how to make that work without referencing the sub-projects in the main project ; the jars of the sub-projects wouldn't be included in the generated jar of the main project when building, and one would be able to add or remove them at will, altering the result of the above code.

If I'm not mistaken, this is the behavior advertised in the Lookup API documentation.
Thanks in advance.

Edit: For now, my conclusion is that without the NetBeans Platform (or OSGi ?) it's not possible to detect which service providers are present at startup time. You need to reference their jars in your classpath, and thus to identify them before startup. Feel free to prove me wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月依秋水 2024-11-17 06:25:34

您必须在调用应用程序中引用子项目,因为这会将其放在类路径上 - 如果 jar/库不在类路径上,则像 Lookup 和 ServiceLoader 这样的 API 将无法找到它。

如果您使用 OSGI 或 NetBeans 平台,这些系统允许您在运行时更改类路径。

Geertjans 博客 有一个关于此内容的条目(在 NetBeans 平台之外使用 Lookup API) ),在他的博客中,他还引用了 John O'Connors 博客,它对比了 ServiceLoader 和 Lookup API

编辑

我刚刚看到乔恩·斯基茨类似问题。
您可以使用 -Djava.ext.dirs=lib 属性将文件夹(在本例中为“libs”)设置为必须在其中查找类路径的 jar 的位置。

You have to reference the sub-project in your calling application, as this puts it on the classpath - If the jar/library is not on the classpath then APIs like the Lookup and ServiceLoader wont be able to find it.

If you use OSGI or the NetBeans platform these systems allow you to change the classpath at runtime.

Geertjans blog has an entry about exactly this(using the Lookup API outside of the NetBeans platform), in his blog he also references John O'Connors blog which contrasts the ServiceLoader and Lookup APIs

EDIT

I've just seen Jon Skeets' answer to a similar question.
You can use the -Djava.ext.dirs=lib property to set a folder (In this case 'libs') as the place where it must lookup jars for your classpath.

╄→承喏 2024-11-17 06:25:34

根据我的理解,您不必将所有模块与主项目捆绑在一起才能工作。您所需要的只是在启动应用程序时确保您的模块位于类路径上,因为全局 Lookup 使用 ServiceLoader 底层机制。根据您的问题,我建议考虑

  • 直接使用 ServiceLoader 是否更适合您的问题或
  • 某些 DI 框架,例如 Guice 值得一试,或者
  • 如果 OSGI 也提供了对您有用的东西并使用它。

不要误解我的意思,我非常喜欢 NetBeans 和 NetBeans 平台,但在我看来,由于上面列出的可能性,单独使用 Lookup 的用途有限。

In my understanding you don't have to bundle all the modules together with the main project for this to work. All you need is to make sure that your modules are on the classpath when starting the application, because the global Lookup uses the ServiceLoader mechanism under the hood. Based on your question I recommend considering if

  • using the ServiceLoader directly is a better match for you problem or
  • some DI framework like Guice is worth a try or
  • if OSGI offers something useful for you as well and use that.

Don't get me wrong, I absolutely love NetBeans and the NetBeans platform, but in my opinion using the Lookup alone is of limited use because of the possibilities listed above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文