Properties 使用属性的Camel文件uri

Properties 使用属性的Camel文件uri,properties,apache-camel,Properties,Apache Camel,我正在尝试使用属性文件从文件夹进行路由: 我的属性文件有一些属性: from.file=D:/Develop/resources 我想在骆驼上下文xml中使用它作为文件路由 我试过: <camel:route id="Main-Route"> <camel:from uri="file:${from.file}" /> <camel:to uri="seda:fooQueue" /> </camel:route>

我正在尝试使用属性文件从文件夹进行路由:

我的属性文件有一些属性: from.file=D:/Develop/resources

我想在骆驼上下文xml中使用它作为文件路由

我试过:

<camel:route id="Main-Route">
        <camel:from uri="file:${from.file}" />
        <camel:to uri="seda:fooQueue" />
</camel:route>

但骆驼给了我一个例外: 不允许使用${}占位符的动态表达式。使用“文件名”选项设置动态表达式


我如何才能做到这一点?

在Camel中,使用{{property}}在路由中注入属性。 请在此阅读更多

您的示例将更改为:

<camel:route id="Main-Route">
        <camel:from uri="file:{{from.file}}" />
        <camel:to uri="seda:fooQueue" />
</camel:route>

您还需要告诉Camel在哪里可以找到您的属性文件。从上面的链接: SpringXML提供了两种可配置的变体。您可以将Springbean定义为类似于JavaDSL的PropertiesComponent。或者你可以使用标签

<bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
    <property name="location" value="classpath:com/mycompany/myprop.properties"/>
</bean>

使用标记使配置更新鲜,例如:

<camelContext ...>
   <propertyPlaceholder id="properties" location="com/mycompany/myprop.properties"/>
</camelContext>

这方面的问题在于,将占位符理解为:

${some property}用于某些bean,例如:

<bean id="bean" class="">
  <property name="field Constructor" value="${some.property}" />
</bean>

我犯了一个错误

通过定义PropertyPlaceHolderConfigure解决了此问题:

<bean id="proprty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <value>file:/D:/Proj/resources/myprop.properties
    </value>
 </bean>

 <bean id="beanId" class="com.viewlinks.eim.properties.MyBean">
    <property name="fieldConstructor" value="${some.property}" />
</bean>


<camel:camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">   

 <propertyPlaceholder id="properties"  location="file:/D:/Proj/resources/myprop.properties"/>

<camel:route id="Main-Route">
  <camel:from uri="file:{{from.file}}" />
  <camel:to uri="file:{{to.file}}" />
</camel:route>

</camel:camelContext>

文件:/D:/Proj/resources/myprop.properties

在apache camel的文件组件中,起始目录不应包含动态表达式。如果您还想提供动态起始目录,您可以将整个路径设置为从属性文件到文件组件的CamelFileName头,其中fileComponent定义为

,我也尝试了,得到了异常:“未能解析端点:文件:{{from.file}”由于:properties必须在CamelContext中定义名为properties的组件以支持属性占位符。“您阅读了我发送给您的链接了吗?”?请参阅我的最新答案。