【问题标题】:Camel and specifying the properties file without hardcoding骆驼并指定属性文件而无需硬编码
【发布时间】:2015-11-26 11:51:34
【问题描述】:

我试图为骆驼相关的东西和其他东西获取一个单独的配置/属性文件。使用蓝图,但我想在这种特殊情况下也可能是 Spring

据我了解,在 Karaf 中将属性占位符用于配置数据是一种很好的做法。将在 ../karaf/ect 文件夹中搜索。

很好,因为我们没有固定的编码路径。我有那部分工作。

<blueprint ...
    <cm:property-placeholder persistent-id="org.example.project" update-strategy="reload" id="prop">
        ...
    </cm:property-placeholder>

    <camelContext id="myCamelContext" xmlns="http://camel.apache.org/schema/blueprint">
        <route id="watchfolder">
            <from uri="file:{{ENDPOINT.IN}}"/>
            <to uri="direct:test"/>
        </route>

../karaf/ect/org.example.project.cfg 文件:

ENDPOINT.IN=C:/Users/username/Documents/project/folderwatch

在camelContext 中,我们也可以使用propertyPlaceholder。如果我这样定义它就可以工作

...
            <propertyPlaceholder id="properties" location="file:C:/Users/username/Documents/project/camel.properties" />
...

并将属性移动到文件 C:/Users/username/Documents/project/camel.properties。

但这是我的蓝图文件中的固定路径。我认为最好将其配置为在不同的开发环境中进行测试/工作/...只需指向不同的配置文件。

我想这样做,但不起作用:

...
        <propertyPlaceholder id="properties" location="file:{{CAMEL.PROPERTIES_FILE}}" />
...

../karaf/ect/org.example.project.cfg 文件:

CAMEL.PROPERTIES_FILE=C:/Users/username/Documents/project/camel.properties

结果是:

..
Caused by: java.io.FileNotFoundException: {{CAMEL.PROPERTIES_FILE}} (The system cannot find the file specified)
..

该值似乎没有被解析为属性。 我尝试了以下变体:

location="file:${CAMEL.PROPERTIES_FILE}" 
location="file:{{blueprint:CAMEL.PROPERTIES_FILE}}" 
location="file:{{blueprint:prop:CAMEL.PROPERTIES_FILE}}" 

(最后的这些尝试只是赌博。我不明白该功能背后的含义)

所以最终的问题是:

如何指定可变骆驼属性文件?

我正在使用:Karaf 4.0.0、Camel 2.15.3、Win7

【问题讨论】:

  • 如果您已经有了蓝图 cfg 文件,为什么还要引入第二个配置文件?只需将所有属性放在 cfg 文件中,并在 Camel 上下文之外使用 ${my.property} 并在 Camel 上下文中使用 {{another.property}} 即可将指定的键替换为 cfg 文件中的值。

标签: spring apache-camel karaf blueprint


【解决方案1】:

有两种方法可以做到这一点

<propertyPlaceholder id="properties" location="${env:PROP_FILE_OS_ENV_LOC}"/>

其中 PROP_FILE_OS_ENV_LOC 是操作系统环境变量

<propertyPlaceholder id="properties" location="${PROP_FILE_JVM_LOC}"/>

其中 PROP_FILE_JVM_LOC 是 JVM 属性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-14
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    相关资源
    最近更新 更多