什么时候应该使用 Import-Package,什么时候应该使用 Require-Bundle?

发布于 2024-08-13 19:45:40 字数 186 浏览 4 评论 0原文

OSGi 允许通过 Import-Package 确定依赖关系,它仅连接单个包(从任何包导出),以及 Require-Bundle,它连接到特定命名包的导出。

在构建新的 OSGi 应用程序时,我应该使用哪种方法来表示依赖关系?大多数捆绑包将是内部的,但会有一些对外部(开源)捆绑包的依赖。

OSGi allows for dependencies to be determined via Import-Package, which just wires up a single package (exported from any bundle), and Require-Bundle, which wires up to a specific named bundle's exports.

In building a greenfield OSGi application, which approach should I use to represent dependencies? Most of the bundles will be internal, but there will be some dependencies on external (open-source) bundles.

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

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

发布评论

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

评论(6

平安喜乐 2024-08-20 19:45:40

我相信 Require-Bundle 是 Eclipse 的东西(现在已经在 OSGi 规范中包含它以适应 Eclipse)。 “纯粹的”OSGi 方法是使用 Import-Package,因为它专门将包与提供它的捆绑包解耦。您应该声明对所需功能(某个包的某个版本提供的 Java API)的依赖关系,而不是该功能的来源(这对您来说不重要)。这使得捆绑包的组成更加灵活。

JavaScript 类比:这就像检测 Web 浏览器是否支持某个 API,而不是根据用户代理字符串推断它是什么类型的浏览器。

OSGi 联盟的 Peter Kriens 在 OSGi 博客

可能唯一需要使用 Require-Bundle 的情况是如果您有拆分包,即分布在多个包中的包。当然,强烈建议不要拆分包。

I believe Require-Bundle is an Eclipse thing (that has now made it in the OSGi spec to accommodate Eclipse). The "pure" OSGi way is to use Import-Package, as it specifically decouples the package from the bundle that provides it. You should be declaring dependencies on functionality that you need (the Java API provided by a certain version of a certain package) instead of where that functionality is coming from (which should not matter to you). This keeps the composition of bundles more flexible.

JavaScript analogy: This is like detecting whether a web browser supports a certain API versus inferring from what the user-agent string says what kind of browser it is.

Peter Kriens of the OSGi Alliance has more to say about this on the OSGi blog.

Probably the only case where you need to use Require-Bundle is if you have split packages, that is a package that is spread across multiple bundles. Split packages are of course highly discouraged.

靑春怀旧 2024-08-20 19:45:40

优先选择 Import-Package 而不是 Require-Bundle。

Require-Bundle:

  • 指定要使用的显式包(和版本)。如果需要重构所需的捆绑包并将包移至其他地方,则依赖项将需要更改其 MANIFEST.MF
  • 使您可以访问该捆绑包的所有导出,无论它们是什么,也无论您是否需要它们。如果您不需要的部分有自己的依赖项,则您需要将这些部分
  • 重新导出到捆绑包,
  • 尽管不鼓励,但允许使用拆分包,即:分布在多个捆绑包中的包
  • 可用于非代码依赖项,例如:资源、帮助等。

Import-Package:

  • 松散耦合,仅指定包(和版本),运行时找到所需的包。
  • 实际实现可以交换出。
  • 依赖包可以通过以下方式移动到不同的包中:包所有者
  • 但需要在较低粒度级别维护更多元数据(即:每个包名称)

Favour Import-Package over Require-Bundle.

Require-Bundle:

  • specifies the explicit bundle (and version) to use. If a requirde bundle needs to be refactored and a package moved elsewhere, then dependents will need changes to their MANIFEST.MF
  • gives you accesss to ALL exports of the bundle, regardless of what they are, and regardless of whether you need them. If the parts you don't need have their own dependencies you will need those to
  • bundles can be re-exported
  • although discouraged, allows the use of split packages, ie: a package that is spread across multiple bundles
  • can be used for non-code dependencies, eg: resources, Help etc.

Import-Package:

  • looser coupling, only the package (and version) is specified and the run-time finds the required bundle
  • Actual implementations can be swaped out
  • Dependent packages can be moved to different bundles by the package owner
  • But requires more metadata to be maintained (i.e: each package name) at lower levels of granularity
顾北清歌寒 2024-08-20 19:45:40

我相信 Import-Package 可以为您提供更松散的耦合,并且应该是首选。我在声明对我不拥有的包(例如 slf4j)的依赖项时使用它,并且我可以根据需要交换实现。当依赖项是我可以控制的东西(例如我自己的捆绑包)时,我使用 Require-Bundle,因为任何重要的更改无论如何都会经过我自己。

I believe Import-Package gives you looser coupling and should be preferred. I use it when declaring dependencies on packages that I don't own, such as slf4j, and I can swap implementations as I wish. I use Require-Bundle when the dependency is something I have control over, such as my own bundles, because any important change would have gone through myself anyway.

不语却知心 2024-08-20 19:45:40

Import-Package 应该更好,因为如前所述,您可以将包从一个包移动到另一个包,而无需更改现有客户端的 MANIFEST.MF

但是...

使用 有一个实际的理由如果您使用 Eclipse 开发捆绑包,则需要 Require-Bundle:

Eclipse 不使用包作为解析单位。它使用捆绑包。也就是说,如果您使用捆绑包中的一个包,Eclipse 会编译您的捆绑包,而不会报告使用未从该捆绑包导入的其余包的任何问题。

您可能(您是人类)认为一切正常并上传您的捆绑包进行部署,但是......您的捆绑包将在运行时损坏。

我确信这一点,因为这个问题今天就发生在(我身上!)。

好的解决方案是更改 Eclipse 类路径容器,但是...如果不这样做...您可以决定避免此类问题,需要捆绑包,而不是包,支付上述价格(没有向后兼容)包之间的代码移动)。

Import-Package should be better because, as previously said, you can move a package from one bundle to another without changing existing client's MANIFEST.MF

But...

There is a practical reason to use Require-Bundle if you are using Eclipse to develop your bundles:

Eclipse don't use packages as units of resolution. It uses bundles. That is, if you use one package of a bundle, Eclipse compiles your bundle without reporting any problem with the use of the rest of packages not imported from that bundle.

You could (you are human) think that everything is OK and upload your bundle for deployment but ... your bundle will break at runtime.

I'm sure about it because this problem has happened (to me!) today.

The good solution would be to change the Eclipse classpath container but... if this is not going to be done... you could decide to avoid this kind of problems requiring bundles, instead of packages, paying the mentioned price (no backward compatible code movement between bundles).

半寸时光 2024-08-20 19:45:40

避免导入包。
由于包提供了捆绑包之间的多对多关系,因此它们很容易出现难以检测和避免的依赖循环。

另一方面,Require-Bundle 引用单个包,通过简单的构建时检查使依赖图免受循环影响。
使用 Require-Bundle,可以更轻松地构建具有隔离的较低抽象级别的分层架构。

Avoid Import-Package.
As packages provide many-to-many relationships between bundles, they are prone to dependency cycles that are hard to detect and avoid.

Require-Bundle on the other hand, references a single bundle, making dependency graph protected from cycles by a trivial build-time check.
With Require-Bundle it is much easier to build layered architecture with isolated lower level of abstraction.

洛阳烟雨空心柳 2024-08-20 19:45:40

我不相信使用 Import-Package 更好,因为我在使用捆绑包时的默认期望是使用关联的公共 API。因此,Require-Bundle 更有意义。

I'm not convinced that using Import-Package is better, because my default expectation when working with a bundle is to work with the associated public API. For that reason, Require-Bundle makes more sense.

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