我强烈建议利用依赖注入来解耦捆绑包OSGi Blueprint(请参阅OSGi Service Platform Enterprise Specification中的Blueprint Container Specification,但请注意它适用于 V5 版本)。
使用规范,您可以使用单独的 xml 文件声明捆绑包的依赖关系 - OSGI-INF/blueprint/*.xml(默认设置,但可以使用Bundle-Blueprint标头更改)。
因此,在您的情况下,您将开发Comm Service其服务是该捆绑包的依赖项的Communication捆绑包。还有Poll可能Postcard注册服务的同一服务接口的捆绑包,以便它们可以成为Communication捆绑包的依赖项。
在这样的配置中,Communication传递Comm Service捆绑/服务的不是捆绑,而是在初始化期间Poll和Postcard捆绑获得它们的Comm Service服务依赖关系。
这是捆绑包的假设蓝图配置Poll。
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean id="PollSenderBean" class="pl.japila.osgi.poll.PollSender">
<argument ref="commService" />
</bean>
<service id="PollSenderBeanService" ref="PollSenderBean"
interface="pl.japila.osgi.Sender">
<service-properties>
<entry key="type" value="poll" />
</service-properties>
</service>
<reference id="commService" interface="pl.japila.osgi.comm.CommService" />
</blueprint>
捆绑包将Postcard是相似的。请注意 的entry值,poll以便在需要此类查询时获得特定服务。
捆绑包的蓝图配置Communication如下:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<reference-list activation="lazy" member-type="service-object"
id="Sender" interface="pl.japila.osgi.Sender" />
</blueprint>
参考规范,也许还有Apache Aries Blueprint 模块来学习和学习解决方案。