您的位置:首页 > 产品设计 > UI/UE

OSGI学习之路之Require-Bundle与Imported-Package

2017-07-15 23:07 381 查看
1       OSGI之Require-Bundle与Imported-Package

OSGI中的bundle在依赖其他的bundle的时候,有两种方式:Require-Bundle与Imported-Package。这里简单的讲讲他们之间的区别。

先看看基础的定义:

•       Required-Bundle: Specify thelist of plug-ins required for the operation of this plug-in.

•       指定操作此插件所需的插件列表。

•       Imported-Package:Specifypackages on which this plug-in depends without explicitly identifying their originatingplug-in.

•       指定此插件所依赖的软件包,而不明确标识其来源插件。

1.1   OSGIClassLoader机制

Many bundles can share a single virtualmachine (VM). Within this VM, bundles can hide packages and classes from otherbundles, as well as share packages with other bundles.

Bundle共享Java虚拟机,这样的不同的bundle之间才能相互引用package。

The key mechanism to hide and sharepackages is the Java class loader that loads classes from a sub-set of thebundle-space using well-defined rules. Each bundle has a single class loader.

理解OSGI的Package import-export机制的关键在于明白每个bundle都有自己的Class Loader。

对于OSGI环境来说,它们是没有区别的。无论是Require-Bundle还是Imported-Package,前提都是需要把相关的packages先Export出。先看看OSGI下的ClassImport-Export关系:

 

1.2   区别

它们之间,仅仅只是依赖的颗粒度的区别。Required-Bundle依赖的是bundle,而Import-Package依赖的是package,而不关心是哪一个bundle export出来的。

这个区别,其实可以导致很大的用法差异。比如:

1.      在OSGI环境中有许多不同的版本的bundle的时候,required-bundle可以精确指定版本依赖,而import-package就不行了,它会根据classloader的顺序选择最先找到的class

2.      对于同一个package被许多不同的bundle同时export的时候,可以考虑使用import-package,它避免了对bundle的依赖
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: