【问题标题】:How to use java properties file in OSGI Declarative Services Annotations如何在 OSGI 声明式服务注释中使用 java 属性文件
【发布时间】:2016-09-09 02:26:20
【问题描述】:

我正在尝试使用 bndtools 来创建我的 OSGI 程序。这是我之前的代码,它可以很好地与 felix 控制台配合使用。

package com.buaa.ate.service.data.command;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.apache.felix.service.command.CommandProcessor;
import com.buaa.ate.service.api.data.Publisher;

@Component(
        service=PublishCommand.class,
        property={
                CommandProcessor.COMMAND_SCOPE + ":String=example",
                CommandProcessor.COMMAND_FUNCTION + ":String=publish",  
        }
)
public class PublishCommand {

    private Publisher publishSvc;
    @Reference
    public void setPublisher(Publisher publishSvc) {
        this.publishSvc = publishSvc;
    }
    public void publish(String content) {
        publishSvc.start();
        long result = publishSvc.publish(content);
        System.out.println(result);
        publishSvc.stop();
    }
}

现在,我想像这样更改注释:

package com.buaa.ate.service.data.command;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.apache.felix.service.command.CommandProcessor;
import com.buaa.ate.service.api.data.Publisher;

@Component(
        service=PublishCommand.class,
        properties="com/buaa/ate/service/data/command/config.properties"
)
public class PublishCommand {

    private Publisher publishSvc;
    @Reference
    public void setPublisher(Publisher publishSvc) {
        this.publishSvc = publishSvc;
    }
    public void publish(String content) {
        publishSvc.start();
        long result = publishSvc.publish(content);
        System.out.println(result);
        publishSvc.stop();
    }
}

这是我的属性文件: config.properties

内容是这样的:

osgi.command.scope\:String:example
osgi.command.function\:String:publish

当我运行程序时,输入命令'publish something',然后问题就出现了:

'gogo: CommandNotFoundException: Command not found: publish'

那么,我应该怎么做才能解决这个问题呢?

【问题讨论】:

    标签: java properties annotations osgi declarative-services


    【解决方案1】:

    嗯,我只是意识到解决问题非常容易。这是 osgi javadoc 的一部分:

    财产

    公共抽象 java.lang.String[] 属性

    此组件的属性。 每个属性字符串都指定为“key=value”。属性值的类型可以在键中指定为 key:type=value。类型必须是组件描述的 property 元素的 type 属性支持的属性类型之一。

    要指定具有多个值的属性,请使用多个键值对。例如,“foo=bar”、“foo=baz”。

    另见: "组件描述的属性元素。"

    默认值:{}

    所以我在config.properties中添加了'type'属性,然后代码就可以正常运行了。这是当前的属性文件: current properties file

    它的内容是这样的:

    osgi.command.scope=example
    osgi.command.scope\:type:String
    osgi.command.function=publish
    osgi.command.function\:type:String
    

    程序现在可以正常运行了。

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 2012-04-27
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      相关资源
      最近更新 更多