1

我想知道是否可以使用声明性服务注释来利用 osgi 企业规范的par.104.7.5(使用多位置)中解释的内容。引用规格:

Bundles 也可能对一个目标服务的多个 PID 感兴趣,因此他们可以为一个服务注册多个 PID。[...]

对主机配置感兴趣的 Bundle 将注册具有以下属性的托管服务:

service.pid = [ "com.acme.host", "com.acme.system" ]

Bundle 将针对 PID 和 PID 被回调,com.acme.host因此com.acme.system必须区分这两种情况。因此,此托管服务将具有如下回调:

volatile URL url;
public void updated( Dictionary d ) {
if ( d.get("service.pid").equals("com.acme.host"))
    this.url = new URL( d.get("host"));
if ( d.get("service.pid").equals("com.acme.system"))
   ...
}

我尝试使用以下语法:

@Component(
    immediate = true,
    configurationPid = "[com.mycompany.ws.rest,com.mycompany.endpoints]",
    configurationPolicy = ConfigurationPolicy.REQUIRE
) 
public class TestImpl implements Test {
    // ...
}

但这失败了。当然,可以参考 config admin 并根据所需的 pid 浏览配置,但这对我来说似乎有点不雅,因为理论上可以将其委托给 ds 注释。

可能吗?什么是正确的语法?

谢谢!

4

3 回答 3

2

我不相信通过使用configurationPidconfigurationPolicy值是可能的。我要做的是:

  • 将服务工厂 pid(s) 定义为服务属性。
  • 实现ManagedService接口。

例子:

@Component(property = {Constants.SERVICE_PID + "=com.acme.host", 
                       Constants.SERVICE_PID + "=com.acme.system"})
public class TestComponent implements ManagedService {

    @Override
    public void updated(Dictionary<String, ?> dict) {
    ...
    }

当然,这样做的缺点是即使没有配置,您的组件也会被激活,但您可以使用两个 PID。

于 2015-07-05T15:19:06.373 回答
0

即将发布的 DS 1.3 规范支持多个 PID。请参阅http://www.osgi.org/Specifications/Drafts以获取下载第 6 版规范草案的链接。

于 2015-07-05T20:22:12.443 回答
0

较新的版本解决了这个问题...

来自:https ://osgi.org/specification/osgi.cmpn/7.0.0/service.component.html#org.osgi.service.component.annotations.Component

112.13.4.11 String[] configurationPid default "$" □ 该组件配置的配置PID。

每个值指定此组件的配置 PID。

如果没有指定值,则使用该组件的名称作为该组件的配置PID。

可以使用特殊字符串(“$”)将组件的名称指定为配置 PID。NAME 常量保存这个特殊的字符串。例如:

@Component(configurationPid={"com.acme.system", Component.NAME}) 从该注解创建组件描述的工具必须用该组件的实际名称替换特殊字符串。

另见 组件描述的组件元素的 configuration-pid 属性。

从 1.2 开始

于 2018-12-22T17:13:24.123 回答