繁体   English   中英

在Adobe CQ5中设置属性

[英]Setting properties in Adobe CQ5

我正在研究基于CQ5的应用程序,这对我来说是一个全新的领域,因为我之前主要研究基于Spring的Web应用程序。

该应用程序是基于Blue-prints原型的maven项目(http://www.cqblueprints.com/xwiki/bin/view/Blue+Prints/The+CQ+Project+Maven+Archetype)。

现在我有一个问题,添加一些属性的标准方法是什么,通常会转到标准web-app中的config.properties(或类似)文件。 包含hostNames,accountNumbers等内容的属性。

干杯。

我不熟悉蓝图,但据我所知,这只是生成CQ项目结构的一种方式,因此我认为它对您管理配置参数的方式没有任何实际影响。

CQ5基于Apache Sling ,它使用OSGi ConfigAdmin服务来配置可配置参数,并提供了一些工具来简化这一过程。

您可以在PathBasedDecorator Sling组件中看到一个示例,它使用@Component批注将自己声明为OSGi组件:

@Component(metatype=true, ...)

然后使用@Property批注声明一个多值可配置参数,默认值为:

@Property(value={"/content:2", "/sling-test-pbrt:2"}, unbounded=PropertyUnbounded.ARRAY)
private static final String PROP_PATH_MAPPING = "path.mapping";

然后在组件的activate()方法中读取该值:

  final Dictionary<?, ?> properties = componentContext.getProperties();
  final String[] mappingList = (String[]) properties.get(PROP_PATH_MAPPING);

并且包含该组件的OSGi包提供了metatype.properties文件,用于定义可配置参数的名称和标签。

就是这样 - 有了这个,Sling和OSGi框架为组件生成一个基本配置UI,您可以从/ system / console / config访问,并在配置参数更改时自动管理组件的激活和重新激活。

这些配置也可以来自JCR存储库,这要归功于Sling安装程序在那里选择它们,你可以在CQ5存储库中的/ libs和/ apps下找到名为“config”的文件夹中的一些。

另一种选择是直接使用JCR内容,具体取决于您的可配置参数的使用方式。 您可以告诉您的组件其配置位于存储库中的/ apps / foo / myparameters下(并使该值可配置),并根据需要在该节点下添加JCR属性和子节点,组件可以读取。 缺点是当参数改变时,@ Component不会自动重启,就像直接使用OSGi配置时一样。

很长的解释...希望这有帮助;-)

非常感谢Bertrand,你的回答确实指出了我正确的方向。

我所做的是为每个运行模式创建了.ConfigService.xml,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"         xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:OsgiConfig"
myconfig.config="{String}My Value"/>

然后在我的ConfigService看起来像这样:

@Component(immediate = true, metatype = true)
@Service(ConfigService.class)
public class ConfigService {

    private Dictionary<String, String> properties;

    @SuppressWarnings("unchecked")
    protected void activate(ComponentContext context) {
        properties = context.getProperties();
    }

    protected void deactivate(ComponentContext context) {
        properties = null;
    }

    public String getProperty(String key) {
        return properties.get(key);     
    }
}

如果我需要使用@Reference获取访问它的配置属性,那么我只使用ConfigService。

我希望能帮助别人!

ConfigService示例可能不是最好的方法,因为ComponentContext只应在组件激活和停用期间依赖。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM