简体   繁体   English

在Adobe CQ5中设置属性

[英]Setting properties in Adobe CQ5

I'm working on CQ5 based app, which is a whole new area for me as I was mainly working on Spring based web-apps before. 我正在研究基于CQ5的应用程序,这对我来说是一个全新的领域,因为我之前主要研究基于Spring的Web应用程序。

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

Now I have a question, what is a standard approach to add some properties, that would normally go to config.properties (or alike) file in standard web-app. 现在我有一个问题,添加一些属性的标准方法是什么,通常会转到标准web-app中的config.properties(或类似)文件。 Properties that contain things like hostNames, accountNumbers and alike. 包含hostNames,accountNumbers等内容的属性。

Cheers. 干杯。

I'm not familiar with blueprints, but as I understand that's just a way to generate your CQ project structure, so I assume it doesn't have any real impact on how you manage configuration parameters. 我不熟悉蓝图,但据我所知,这只是生成CQ项目结构的一种方式,因此我认为它对您管理配置参数的方式没有任何实际影响。

CQ5 is based on Apache Sling , which uses the OSGi ConfigAdmin service for configurable parameters, and provides a few tools to make this easier. CQ5基于Apache Sling ,它使用OSGi ConfigAdmin服务来配置可配置参数,并提供了一些工具来简化这一过程。

You can see an example of that in the PathBasedDecorator Sling component, which uses the @Component annotation to declare itself as an OSGi component: 您可以在PathBasedDecorator Sling组件中看到一个示例,它使用@Component批注将自己声明为OSGi组件:

@Component(metatype=true, ...)

and later uses an @Property annotation to declare a multi-value configurable parameter, with default values: 然后使用@Property批注声明一个多值可配置参数,默认值为:

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

That value is then read in the component's activate() method: 然后在组件的activate()方法中读取该值:

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

and the OSGi bundle that contains that component provides a metatype.properties file to define the name and label of the configurable parameter. 并且包含该组件的OSGi包提供了metatype.properties文件,用于定义可配置参数的名称和标签。

That's it - with this, Sling and the OSGi framework generate a basic config UI for the component, that you can access from /system/console/config, and manage your component's activation and reactivation automatically if configuration parameters change. 就是这样 - 有了这个,Sling和OSGi框架为组件生成一个基本配置UI,您可以从/ system / console / config访问,并在配置参数更改时自动管理组件的激活和重新激活。

Those configurations can also come from the JCR repository, thanks to the Sling installer which picks them up there, you can find a number of those in folders named "config" under /libs and /apps in your CQ5 repository. 这些配置也可以来自JCR存储库,这要归功于Sling安装程序在那里选择它们,你可以在CQ5存储库中的/ libs和/ apps下找到名为“config”的文件夹中的一些。

Another option is to use JCR content directly, depending on how your configurable parameters are used. 另一种选择是直接使用JCR内容,具体取决于您的可配置参数的使用方式。 You could tell your component that its config is under /apps/foo/myparameters in the repository (and make just that value configurable), and add JCR properties and child nodes under that node as needed, that your component can read. 您可以告诉您的组件其配置位于存储库中的/ apps / foo / myparameters下(并使该值可配置),并根据需要在该节点下添加JCR属性和子节点,组件可以读取。 The disadvantage is that your @Component won't be restarted automatically when parameters change, as happens when using OSGi configurations directly. 缺点是当参数改变时,@ Component不会自动重启,就像直接使用OSGi配置时一样。

Long explanation...hope this helps ;-) 很长的解释...希望这有帮助;-)

Thanks a lot to Bertrand, your answer really pointed me in the right direction. 非常感谢Bertrand,你的回答确实指出了我正确的方向。

What I did was I created .ConfigService.xml for each of my ran modes, which looks like that: 我所做的是为每个运行模式创建了.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"/>

Then in my ConfigService looked like that: 然后在我的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);     
    }
}

Than I just use ConfigService if I need to get a config property accessing it using @Reference. 如果我需要使用@Reference获取访问它的配置属性,那么我只使用ConfigService。

I hope that can help someone! 我希望能帮助别人!

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

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

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