从java访问Karaf cfg文件中的属性?

时间:2015-11-20 13:17:52

标签: osgi config karaf blueprint property-placeholder

我在蓝图中使用cm:property-placeholder来加载配置文件:

<!-- External configuration -->
<cm:property-placeholder persistent-id="mubyndle" update-strategy="reload">
    <cm:default-properties>
        <cm:property name="schemaValidation" value="false"/> 
    </cm:default-properties>
</cm:property-placeholder>

<bean id="myBean" class="com.mybean">
    <property name="abc" value="${abc}" />
</bean>

我可以将值设置为bean,例如$ {abc}。
如何从java访问配置的任何其他属性?

我打算添加属性列表: 为prop1 = 11 PROP2 = 22 ... propn = NN

由于数字不同,我无法在蓝图中添加所有内容。

谢谢你, 尤

3 个答案:

答案 0 :(得分:2)

在蓝图中添加对ConfigurationAdmin服务的引用:
    <reference id="configAdmin" interface="org.osgi.service.cm.ConfigurationAdmin" />

将其设置为bean:
<bean id="myBean" class="com.mybean"> <property name="abc" value="${abc}" /> <property name="configAdmin" ref="configAdmin" /> </bean>

在豆中:
private ConfigurationAdmin configAdmin;

public void setConfigAdmin(ConfigurationAdmin configAdmin) throws IOException {
    this.configAdmin = configAdmin;

    System.out.println(configAdmin);
    System.out.println(configAdmin.getConfiguration("any-persistent-id").getProperties());
}

答案 1 :(得分:1)

您可以在org.apache.aries.blueprint.compendium.cm.CmPropertyPlaceholder<property name="cmpp" ref="cmpp" />后将<cm:property-placeholder id="cmpp" ...确定为my public void setProperties(CmPropertyPlaceholder properties) { Configuration config = CmUtils.getConfiguration(properties.getConfigAdmin(), properties.getPersistentId()); if (config != null) { Dictionary props = config.getProperties(); System.out.println(props); }

然后在myBean中为cmpp添加setter:
org.apache.aries.blueprint.cm

唯一的问题是bundle org.apache.aries.blueprint.compendium.cm没有导出包CmPropertyPlaceholder,因此我无法访问java中的get_cachedir_bsig()。在karaf中是否有任何命令可以另外导出捆绑包?

可以通过上下文访问配置,将查看并发布。

答案 2 :(得分:0)

你最好这样做:

a)在blueprint.xml

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.1.0"
    xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"

...

<cm:property-placeholder persistent-id="<name of cfg file>"
    update-strategy="reload">
    <cm:default-properties>
        <cm:property name="<property name in file>" value="<value>" />
    </cm:default-properties>
</cm:property-placeholder>


<bean id="<name of Bean>" class="<full name of class>" >
    <property name="<property name in file>" value="${<value>}"></property>
</bean>

b)内部&#34; bean&#34;

  • 定义公共构造函数(Spring所需的定义)
  • 定义一个变量及其Setter:
private <Type of> <name of variable>;
public <Constructor> (){} // no parameters
public void  set<property name in file first upper letter>(<Type of> <property name in file>) {
  <name of variable> = <property name A>;
}
c)加入&#39; / etc&#39;文件夹配置文件:

  

<name of cfg file>.cfg

显然,在其中,属性:

  

<Property name> = <value>