9.6. 在功能中添加 OSGi 配置

如果您的应用程序使用 OSGi 配置管理员 服务,您可以使用功能定义中的 config 子元素指定该服务的配置设置。例如,要指定 prefix 属性的值为 MyTransform,请将以下配置子元素添加到您的功能配置中:

<?xml version="1.0" encoding="UTF-8"?>
<features name="MyFeaturesRepo">
  <feature name="example-camel-bundle">
    <config name="org.fusesource.fuseesb.example">
      prefix=MyTransform
    </config>
  </feature>
</features>

其中 config 元素的 name 属性指定属性设置 的持久 ID (其中持久性 ID 充当属性名称的名称范围)。config 元素的内容的解析方式与 Java 属性文件 相同。

config 元素中的设置可以被 InstallDir/etc 目录中的 Java 属性文件中的设置覆盖,该文件以持久 ID 命名,如下所示:

InstallDir/etc/org.fusesource.fuseesb.example.cfg

例如,在实践中使用上述配置属性的示例,请考虑以下 Blueprint XML 文件来访问 OSGi 配置属性:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0">

    <!-- osgi blueprint property placeholder -->
    <cm:property-placeholder id="placeholder"
                             persistent-id="org.fusesource.fuseesb.example">
        <cm:default-properties>
            <cm:property name="prefix" value="DefaultValue"/>
        </cm:default-properties>
    </cm:property-placeholder>

    <bean id="myTransform" class="org.fusesource.fuseesb.example.MyTransform">
      <property name="prefix" value="${prefix}"/>
    </bean>

</blueprint>

当此蓝图 XML 文件部署到 example-camel-bundle 捆绑包中时,属性引用 ${prefix} 将替换为值 MyTransform,由功能存储库中的 config 元素指定。