java - 如何在 OSGI 声明式服务中使用 @Property 注释的基数
问题描述
我正在将 Apache Felix SCR 注释迁移到 OSGI 声明式服务 [AEM]。虽然 Migration 我找不到 DS 中基数的确切替代品。
现有的 SCR 实施:
@Component (ds = true, immediate = true, metatype = false, policy = ConfigurationPolicy.OPTIONAL)
@Service (SampleService.class)
public class SampleServiceImpl implements SampleService
{
private static final int VECTOR = Integer.MIN_VALUE + 1;
@Property (value = REPOSTING_PATTERN, cardinality = VECTOR,description = "Event reposting pattern for QueuePosting ")
private static String EVENT_REPOSTING_PATTERN = "eventRepostingPattern";
}
现在它在 OSGi 声明式服务中迁移如下
@Component (configurationPolicy = ConfigurationPolicy.OPTIONAL, immediate = true, service =SampleService.class,
property = {SampleServiceImpl .EVENT_REPOSTING_PATTERN +"="+SampleServiceImpl .EVENT_REPOSTING_PATTERN_VALUE })
public class SampleServiceImpl implements SampleService
{
private static String EVENT_REPOSTING_PATTERN = "eventRepostingPattern";
private static String EVENT_REPOSTING_PATTERN = "eventRepostingPatternValue";
}
在 DS 注释实现中,我必须如何映射@Property中存在的参数基数。请建议我
解决方案
关于@Property
and的文档cardinality
像往常一样令人困惑,但我基于此假设这些在某种程度上与配置相关。@Designate
您可以使用和@ObjectClassDefinition
注释为您的服务设置类型安全的配置。该cardinality
选项可以在@AttributeDefinition
注释中找到。
ExampleServiceimpl.java
import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.Designate;
@Component( immediate = true, service = ExampleService.class )
@Designate( ocd = ExampleServiceConfig.class )
public class ExampleServiceImpl implements ExampleService {
@Activate
public void activateService(BundleContext context, ExampleServiceConfig config){
System.out.println(config.some_config());
}
}
ExampleServiceConfig.java
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
@ObjectClassDefinition(
name = " Example service Configuration"
)
public @interface ExampleServiceConfig {
@AttributeDefinition( cardinality = 1 )
String some_config() default "default value";
}
现在作为免责声明,我自己从未设法使用@AttributeDefinition
或cardinality
注释过,所以可能会在这里关闭。cardinality
当我尝试@AttributeDefinition
使用它来查看是否可以使用它来使我的服务配置在 hawtio 中变得漂亮但可能缺少一些步骤时遇到了该选项。
但希望这能为您指明正确的方向。
推荐阅读
- php - 一台带有 nginx 的服务器中的多个 wordpress
- rabbitmq - RabbitMQ 配置为使用自定义的 epmd 端口,但服务器仍然在默认的 epmd 进程上启动
- python - 当随机数达到0时如何结束循环
- java - 休眠搜索 lucene 消耗数据库连接
- python - 使用 python 在 Azure Functions 中进行路由
- javascript - 将 JSON 数据输出到从 FireBase 数据库中提取的道具中
- r - 使用 dplyr (或其他东西)在 R 中重新编码
- bash - 在 bash 中为第一列中的每个不同值查找第 n 列中的最大值
- python - 检查字符串是否有资格从字符串中提取文本 - 正则表达式
- java - ActiveMQ 队列的活动消费者过多