【问题标题】:@Properties in OSGiOSGi 中的@Properties
【发布时间】:2015-01-10 19:04:12
【问题描述】:

我正在编写一项服务并观察到@Property,我们也可以在服务内部定义,如下面的代码:

@Component(metatype = true, immediate = true, description = "Demo Service to test")
@Service(value = DemoService.class)
@Properties({
        @Property(name = "testprop" , value = "This is Test Property")
})
public class DemoServiceImpl implements DemoService {
     @Property(name = "localprop", value = "Local Value")
    @Activate
    public void activate(ComponentContext ctx)
    {
        String testprop = (String)ctx.getProperties().get("testprop");
        String localprop = (String)ctx.getProperties().get("localprop");
    }
}

这两个属性都显示在 felix 控制台中,并可在我的服务中访问。那么是什么造成了在组件内部或外部声明它们的区别。我看到我们不能在组件内部使用@Properties。但不确定是什么让它们在功能上彼此不同以及何时使用它们。

【问题讨论】:

    标签: osgi aem apache-felix


    【解决方案1】:

    我猜你说的是 felix 注释。

    您可以在任何方法或成员变量前使用@Property。但是,这并不意味着将调用该方法或设置变量。唯一的好处是,如果你有一个特定的变量来保存属性的值(通过在 activate 方法中分配它),你的类可以根据注释更加自我解释。

    另一方面,您可以在@Properties 注释中列出属性。我更喜欢这种方式,因为在这种情况下,我可以准确定义属性的顺序,它们应该如何出现在生成的元类型 xml 文件中(以及在 web 控制台上)。

    也可能发生,配置属性没有分配给任何成员变量,它仅在激活方法中使用。在这种情况下,定义它的最佳位置是在类前面的 @Properties 注释内。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-03
      • 2019-12-27
      • 2023-03-06
      • 1970-01-01
      • 2017-07-04
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      相关资源
      最近更新 更多