【问题标题】:PropertyPlaceholderConfigurer inside routeContext. Can't set uri from property filerouteContext 中的 PropertyPlaceholderConfigurer。无法从属性文件设置 uri
【发布时间】:2012-06-05 11:18:38
【问题描述】:

我有属性文件,我想通过这个文件在我的路由中设置 uri:

   <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

        <import resource="TransactionsParserConfig.xml"/>

        <bean id="transactionsValidator" class="com.class.TransactionsValidator"/>
        <bean id="transactionsValidator123" name="${ftp.host}"/> <!--here I can use property-->


        <routeContext id="transactionsRoutes" xmlns="http://camel.apache.org/schema/spring">
            <route id="routeFTP">
                <from uri="direct:start" />
<!--here I can NOT use property-->
                <from uri="ftps://${ftp.host}/?securityProtocol=SSL"/>

etc...

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <array>
                <value>classpath:/ftp.properties</value>
            </array>
        </property>
    </bean>
</beans>

但似乎不允许从属性文件设置 uri。因为当我在 routexContext 标记之外使用 ${ftp.host} 时,一切都很好。当我在 routeContext 中点击 ctrl+mouseclick 时,即使是 Idea 也无法重定向我。 为什么?

【问题讨论】:

    标签: java spring properties


    【解决方案1】:

    由于 Spring 的限制,Camel 无法在其 Spring DSL 中使用 PropertyPlaceholderConfigurer。有很多方法可以实现你想要的。其中一些由link 描述。

    【讨论】:

    • 我按照您的建议使用了 propertyPlaceholder,但我无法让它在资源中找到我的文件。现在我必须将文件放在包含以下源文件的文件夹中:。但我想使用这样的路径: location="classpath:src/test/resources/ftp.properties
    • @DmitryBorovoy 如果您想使用“classpath:”前缀 - 忘记指定文件夹。 Maven 将资源和编译的类文件夹合并到一个类路径中,因此您可以尝试类似“classpath:ftp.properties”
    【解决方案2】:

    你可以使用下面提到的bean配置

    <bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>file:/properties1.properties</value>
                <value>file:/properties2.properties</value>
            </list>
        </property>
    </bean>
    

    属性可以在java类中访问,比如

    @Value("${categories.insert.sql}")
    private String insertQuery;
    

    属性可以在骆驼上下文中访问,例如

    <setHeader headerName="signature">
        <constant>{{api.secretkey}}</constant>
    </setHeader>
    

    希望这可行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 2013-06-30
      • 2018-05-15
      相关资源
      最近更新 更多