首页 > 解决方案 > 如何在同一个插件中处理 2 个 OSGi 声明式服务包组件?

问题描述

我通过手动组件定义使用 OSGi 服务。服务组件由 XML 描述和对象组成。在我尝试在同一个插件中实例化另一个服务之前,我的项目运行良好。现在在我看来,好像我不应该在同一个插件中声明两个 component.xml 文件。

组件.xml

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="ICustomerOSGiService">
   <implementation class="de.checkpoint.rinteln.service.customer.service.CustomerOSGiService"/>
   <service>
    <provide interface="de.checkpoint.rinteln.carlofon.common.service.ICustomerOSGiService"/>
   </service>
</scr:component>

通过注入接口,我可以访问实现。

现在我想要一个具有不同实现的第二个 component.xml,这样我就可以像第一个一样调用。但 Eclipse 不会让我这样做。所以我想,我需要把它们分开。我的意思是在 2 个不同的插件中,到目前为止一直运行良好。不过,我的插件现在看起来很空。所以我想将所有服务组合在同一个插件中。有没有办法将组件集中为 XML?类似于下面的代码(顺便说一下,我已经尝试过,但不幸的是,它不起作用)

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="IOSGiService">
   <implementation class="de.checkpoint.rinteln.service.customer.service.CustomerOSGiService"/>
   <service>
    <provide interface="de.checkpoint.rinteln.carlofon.common.service.ICustomerOSGiService"/>
   </service>

   <implementation class="de.checkpoint.rinteln.service.customer.service.ReminderOSGiService"/>
   <service>
    <provide interface="de.checkpoint.rinteln.carlofon.common.service.IReminderOSGiService"/>
   </service>
</scr:component>

标签: osgiinjecte4declarative-services

解决方案


您可以在一个插件中包含任意数量的组件(我在一个插件中有 8 个)。

您将每个组件放在一个单独的 XML 文件中(名称可以是您想要的任何名称)并将它们列在Service-ComponentMANIFEST.MF 的条目中。

所以在 MANIFEST.MF 我有:

Service-Component: OSGI-INF/playerStateService.xml,
 OSGI-INF/editorManager.xml,
 OSGI-INF/viewManager.xml,
 OSGI-INF/dateUtil.xml,
 OSGI-INF/preferenceSettings.xml,
 OSGI-INF/dialogSettings.xml,
 OSGI-INF/extensionFactory.xml,
 OSGI-INF/imperativeExpressionManager.xml

我的 XML 文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" enabled="true" name="greg.music.playerStateService">
   <implementation class="greg.music.core.services.PlayerStateContextFunction"/>
   <property 
       name="service.context.key" 
       type="String" 
       value="greg.music.core.services.IPlayerStateService"/>
   <service>
      <provide interface="org.eclipse.e4.core.contexts.IContextFunction"/>
   </service>
</scr:component>

(忽略该property值,仅针对此特定服务)。


推荐阅读