已发布的 OSGI 服务是否需要导出其包?
是否需要导出包含 OSGI 服务的包才能发布到服务注册表?或者发布到服务注册表是否可以避免导出包的需要?
Do the packages that contain OSGI services need to be exported in order to publish to the service registry? Or does publishing to the service registry get round the need to export the package?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的回答;第一个问题 = 不,第二个问题 = 不,没有“解决方法”
“合同设计”原则开始发挥作用。基本上,我们提供一个接口供消费者依赖,并为提供者实现——这将我们的消费者代码与实现者的实现细节解耦。请参阅http://www.osgi.org/blog/2011/ 08/package-as-contract.html 以获得良好的概述和 http://en.wikipedia.org/wiki/Design_by_contract。
给定接口(我们希望使用的 API)、实现(API 接口的具体实现)和使用者(希望使用接口实现的代码),使用 OSGi,您有多种选择。
第 2 种方法的优点包括允许多个提供程序(OSGi 使用 LDAP 过滤器,可用于指定与已注册服务的属性匹配的元数据),以及利用 OSGi 的动态绑定特性(即您可以在运行时更改实现提供程序)。
需要记住的一件事是,消费者和提供者必须使用包版本和导出包定义的相同类(换句话说,加载 API 类的包类加载器对于消费者和提供者必须是相同的)。
Short answers; 1st question = no, 2nd question = no, there's no "workaround"
The principle of "Design by contract" comes into play. Basically we provide an interface for consumers to depend on and providers to implement - this decouples our consumer code for the implementation details of our implementors. See http://www.osgi.org/blog/2011/08/package-as-contract.html for a good overview and http://en.wikipedia.org/wiki/Design_by_contract.
Given the interface (the API we wish to use) and the implementation (the concrete implementation of the API interface) and the consumer (code wishing to use an implementation of the interface), with OSGi you have a number of options.
The advantages of number 2 include allowing multiple providers (OSGi uses LDAP filters that can be used to specify metadata matching on the registered services' properties), and leveraging the dynamic binding nature of OSGi (i.e. you can change implementation provider at runtime).
One thing to bear in mind, is that the consumer and provider must use the same classes as defined by the package version and exporting bundle (in other words the bundle classloader that loads the API classes must be the same for both consumer and provider).
服务的类型应该位于导出的包中。服务是有类型的,并建立在模块层的类型共享之上。因此,您需要确保服务的提供者和服务的使用者都可以共享相同的类型。因此,某些bundle必须导出包含该类型的包,以便提供者和消费者可以导入该包。这允许框架验证服务的类型安全性。
The types of the service should be in exported packages. Services are typed and build upon the type sharing of the module layer. So you need to make sure that the provider of the service and the consumers of the service can all share the same type. So some bundle must export the package containing the type so that the provider and the consumers can import the package. This allows the framework to verify type safety of the service.