如何构建具有依赖关系的 OSGi 服务并通过 DS 发布

发布于 2024-09-08 09:56:46 字数 566 浏览 9 评论 0原文

我的示例类:

public class MyModel implements Model
{
    :
    :
}

public class SingleModelProvider implements ModelProvider
{
    public SingleModelProvider(Model providedModel, List actions)
    {
          :
    }
}

计划是在多个包中重用 SingleModelProvider 类,以提供 ModelProvider 的不同实现。我需要在每个包中完成的只是使用构造函数的适当参数来实例化 SingleModelProvider。使用任何 DI 框架的非常简单的场景。如果可能的话,我想使用 DS(声明性服务)注册 ModelProvider 服务,而无需在激活器中编写样板代码。

这可能吗?

我似乎找不到任何有关如何完成此操作的文档,因为 DS 中的类声明似乎不允许使用构造函数参数(或与此相关的设置器)。

我使用工厂吗?我不确定这是否值得,因为它可能不会比手动使用激活器和发布服务更简单。

My sample classes:

public class MyModel implements Model
{
    :
    :
}

public class SingleModelProvider implements ModelProvider
{
    public SingleModelProvider(Model providedModel, List actions)
    {
          :
    }
}

The plan is to reuse the SingleModelProvider class in several bundles to provide different implementations of the ModelProvider. What I need to accomplish in each bundle is to simply instantiate the SingleModelProvider with the appropriate parameters to the constructor. A pretty simple scenario using any DI framework. I would like to register the ModelProvider services using DS (Declarative Services) if possible without having to write the boilerplate code in an Activator.

Is this possible?

I can't seem to find any documentation on how to accomplish this since the class declaration in DS does not seem to allow for constructor arguments (or setters for that matter).

Do I use a factory? I am not sure if that is worthwhile since it may make the case no simpler than using an Activator and publishing service manually.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高速公鹿 2024-09-15 09:56:46

DS 确实支持 setter。以下是基于您问题中的示例的 DS xml 示例。

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="SampleModelProvider">
   <implementation class="test.SampleModelProvider"/>
   <reference bind="setModel" cardinality="1..1" interface="test.Model" name="Model" policy="static" unbind="unsetModel"/>
   <reference bind="setList" cardinality="1..1" interface="test.ActionList" name="ActionList" policy="static" unbind="unsetList"/>
   <service>
      <provide interface="test.ModelProvider"/>
   </service>
</scr:component>

使用构造函数参数在某种程度上违背了 OSGi 的动态特性。服务和捆绑包可以随时启动和停止。 OSGi 友好的代码需要理解这一点,并具有对称方法来处理依赖项的设置和取消设置。

有一个问题要问您:在您的系统中,谁负责创建您希望每个提供者接收的模型对象和操作列表?它们可以作为 OSGi 服务使用吗?我提供的示例 DS 假定它们是 OSGi 服务。

DS does support setters. Here is an example of the DS xml based on the example in your question.

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="SampleModelProvider">
   <implementation class="test.SampleModelProvider"/>
   <reference bind="setModel" cardinality="1..1" interface="test.Model" name="Model" policy="static" unbind="unsetModel"/>
   <reference bind="setList" cardinality="1..1" interface="test.ActionList" name="ActionList" policy="static" unbind="unsetList"/>
   <service>
      <provide interface="test.ModelProvider"/>
   </service>
</scr:component>

Using constructors arguments goes somewhat against the dynamic nature of OSGi. Services and bundles can be started and stopped at anytime. OSGi friendly code needs to understand this and have symmetric methods for handling the setting and unsetting of the dependencies.

One question for you: In your system, who is responsible for creating the Model objects and List of actions that you want each provider to receive? Are they available as OSGi services? The example DS that I provided assumes that they are OSGi services.

高速公鹿 2024-09-15 09:56:46

您想使用 DS 有什么具体原因吗?

您还可以使用 OSGI 蓝图服务,如 OSGI 服务纲要版本 4.2< 中所述。 /a>, 121. 它提供了两个世界中最好的:DI 和简单的服务发布/消费。

据我所知,在 DS 中,唯一的选择是使用工厂,绑定/取消绑定方法不接受用户类。 (如 OSGI 服务纲要版本 4.2 中所述,112.4.5 )

Is there a specific reason why you want to use DS?

You could also use the OSGI Blueprint services as described in the OSGI Service Compendium version 4.2, 121. It provides the best of two worlds: DI and easy service publishing / consuming.

In DS the only option as far as i know is to use the factory, the bind / unbind methods don't accept user classes. (As described in the OSGI Service Compendium version 4.2, 112.4.5)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文