将sling:OsgiConfig读入@ObjectClassDefinition

问题描述

我们有一个Osgi服务(使用R7 DS注释)。我们将OCD用作服务中的内部类。

此外,我们希望组件从JCR中的预定义sling:osgiconfig节点读取属性

已根据需要定义了配置策略。

当组件/服务加载时,它进入“无配置”状态。

需要帮助来从sling:osgiCongig节点读取这些配置。

解决方法

sling:OsgiConfig配置应使用应该使用OCD配置的服务实现类名称进行定义,而不应使用OCD名称进行定义。

OCD基本上是可以具有多种实现的接口。因此,必须使用服务实现类名称定义sling:OsgiConfig节点。例如,假设您有一个SampleServiceImpl.class定义了一个SampleOCDConfig.class,如下所示:

@Component(service = SampleService.class)
@Designate(ocd = SampleServiceImpl.Config.class)
public class SampleServiceImpl implements SampleService {

    public static final String DEFAULT_CUSTOM_CONFIG = "default configuration value";

    @ObjectClassDefinition(name = "Sample OCD Configuration")
    @interface Config {

        @AttributeDefinition(name = "The custom config",defaultValue = DEFAULT_CUSTOM_CONFIG)
        String custom_config() default DEFAULT_CUSTOM_CONFIG;

    }
...

现在,当您为上述设置定义配置时,必须按如下所示进行定义:

/apps/your_project/config/your.sample.service.package.path.SampleServiceImpl.xml

具有以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    jcr:primaryType="sling:OsgiConfig"
    custom.config="default configuration value" />