什么时候应该使用 Import-Package,什么时候应该使用 Require-Bundle?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我相信 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 useImport-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.优先选择 Import-Package 而不是 Require-Bundle。
Require-Bundle:
Import-Package:
Favour Import-Package over Require-Bundle.
Require-Bundle:
Import-Package:
我相信 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.
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.MFBut...
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).
避免导入包。
由于包提供了捆绑包之间的多对多关系,因此它们很容易出现难以检测和避免的依赖循环。
另一方面,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.
我不相信使用 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.