簡體   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