【问题标题】:How to reference a common properties file from within an osgi bundle properties file?如何从 osgi 捆绑属性文件中引用公共属性文件?
【发布时间】:2023-03-23 08:08:01
【问题描述】:

目前,我有许多引用自命名属性文件的功能/捆绑包。我想创建一些公共属性文件,它们包含在单独的捆绑属性文件中。

我知道这在核心 Java 中不起作用,但我确定我已经在某处读过可以在 Karaf/OSGi/Blueprint 中包含属性文件的内容,但现在我想做,但我找不到我一直在搜索的任何网站上的任何参考资料。

我在任何地方都找不到是如何做到这一点的语法。任何人都可以确认 if 这是可能的,如果是这样,这样做的语法是什么?也欢迎任何指向合适文档的指针。

谢谢!

【问题讨论】:

  • 你看过属性占位符吗?您可以在 etc 文件夹中有一个 .cfg 文件,并且可以在蓝图文件或骆驼上下文等中引用属性。
  • 我无法修改属性占位符,因为蓝图是自动生成的。我似乎记得在某处看到了一种将一个 .cfg 文件包含在另一个文件中的方法,但我现在找不到这个。
  • 你是如何生成你的蓝图文件的?有什么参考资料或例子吗?

标签: apache-karaf blueprint-osgi


【解决方案1】:

假设您正在为您的捆绑包使用蓝图:

<?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.3.0"
           xmlns:camel="http://camel.apache.org/schema/blueprint"
           xsi:schemaLocation="
             http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
             http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd
             http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.3.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.3.0.xsd
             ">
    <cm:property-placeholder persistent-id="my.shared" update-strategy="reload" >
        <cm:default-properties>
            <cm:property name="SOURCE" value="DB" />
            <cm:property name="ENV" value="test" />
        </cm:default-properties>
    </cm:property-placeholder>
</blueprint>

为此,您可以在${KARAF_HOME}/etc 目录中拥有一个my.shared.cfg(查看cm:property-placeholder 标签中属性persistent-id 的值)文件。

my.shared.cfg 文件将包含:

SOURCE=DB
ENV=test

假设您想使用此值来实例化使用蓝图中的 bean,您可以执行以下操作:

<bean id="myCustomBean" class="com.example.CustomBeanClass">
    <argument value="${SOURCE}" index="0" />
    <argument value="${ENV}" index="1" />
</bean>

您可以参考here获取信息。

如果这没有帮助,请告诉我。

问候, 库沙尔。

【讨论】:

    【解决方案2】:

    您可以使用 cm:property-placeholder 来获取属性文件值, 我用蓝图开发了一个简单的例子

    <?xml version="1.0" encoding="UTF-8"?>
    <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="  http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd     http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
    <cm:property-placeholder
        persistent-id="org.usr.config" />
    <camelContext id="log-example-context"
        xmlns="http://camel.apache.org/schema/blueprint">
        <route id="log-route">
            <from id="_from1" uri="timer:foo?repeatCount=1" />
            <log id="_log1" message="logging the property value::" />
            <log id="_log1" message="{{conf.data}}" />
        </route>
    </camelContext>
    

    org.usr.config 应作为org.usr.config.cfg 放在 $KARAF_HOME/etc/ 中

    属性可以如下所示

     conf.data=Chandra
    

    你可以参考我的例子GIT

    您可以在Apache community document获取更多知识

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 2014-10-10
      相关资源
      最近更新 更多