什么时候应该使用Require

什么时候应该使用Require

本文介绍了什么时候应该使用Import-Package,什么时候应该使用Require-Bundle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OSGi允许通过Import-Package(仅连接单个软件包(从任何捆绑软件导出))和Require-Bundle(其关联到特定命名捆绑软件的出口)确定依赖关系.

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.

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

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.

推荐答案

我相信Require-Bundle是Eclipse的东西(现在已在OSGi规范中使其成为可容纳Eclipse的东西). 纯" OSGi方法是使用Import-Package,因为它专门将软件包与提供软件包的捆绑包分离.您应该声明对所需功能的依赖(某个程序包的某个版本提供的Java API),而不是该功能的来源(对您而言不重要).这样可以使束的组成更加灵活.

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类比:这类似于检测Web浏览器是否支持某个API,而不是根据用户代理字符串的含义推断出它是哪种浏览器.

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.

OSGi联盟的Peter Kriens在 OSGi博客.

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

可能唯一需要使用Require-Bundle的情况是,如果您有拆分包,则该包分布在多个包中.强烈建议不要使用拆分软件包.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 22:50