使用Camel加载多个属性文件

时间:2013-10-24 19:58:39

标签: xml spring apache-camel

所以我承认我的applicationContext.xml中有很多内容,但我不知道为什么我不能添加另一个名为publish.properties的属性文件,并以类似于a的方式使用它我现在有config.properties。我也有一个sensor.properties但是它有一组keyvalue对(在任何情况下它们都加载得很好)。

我一直在阅读Camel属性文档,即http://camel.apache.org/properties.html,但我仍然不清楚如何指定多个属性文件,以便Camel可以解析它们。

这是我当前运行的applicationContext.xml,并且正好注入config.properties和sensor.properties:

<?xml version="1.0" encoding="UTF-8"?>

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

<bean
    class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<context:component-scan base-package="com.data.world2" />
<context:annotation-config />

<camel:camelContext id="HelloWorldContext">

    <!-- Add Jackson library to render Java Map into JSON -->
    <camel:dataFormats>
      <camel:json id="jack" library="Jackson"/>
    </camel:dataFormats>

    <camel:route>
        <!-- sends {{config.numSamples}} request(s) to the hello world JMS queue every {{config.timeout}} seconds -->

        <camel:from
            uri="timer://hello.world.request.timer?fixedRate=true&amp;period={{config.timeout}}&amp;repeatCount={{config.numSamples}}"/>
        <camel:to uri="log:hello.world.request?level=INFO&amp;showAll=true" />
        <camel:bean ref="helloWorld" />

        <!-- now print out the map in JSON format -->
        <camel:marshal ref ="jack"/>
        <camel:convertBodyTo type="java.lang.String" />
        <camel:log message="${body}"/> 

        <!-- print out message that we are returning sensor event in JSON -->
        <camel:log message="Returned Random Sensor Event in JSON"/>

        <!-- print out values read from config.properties file -->
        <camel:log message="printing values read from config.properties file"/>
        <camel:log message="   config.timeout= {{config.timeout}} milliseconds"/> 
        <camel:log message="   config.numSamples= {{config.numSamples}} random Sensor Event(s)  ## NOTE: 0 or -1 means generate forever ##"/>
        <camel:log message="   config.defaultViz= {{config.defaultViz}}"/>

        <!-- now log the message -->
        <camel:to uri="log:hello.world.response?level=INFO&amp;showAll=true" />

        <!-- now send the message to the JMS queue -->
        <camel:to uri="jms:queue:helloworld.response" />

    </camel:route>

</camel:camelContext>

<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<util:properties id="sensorProperties" location="classpath:/sensor.properties"/>

<!--  pass in sensor.properties and defaultViz from config.properties -->
<bean class="com.data.world2.SensorEventStore">
    <property name="sourceProperties" ref="sensorProperties" />
    <property name="defaultViz" value="${config.defaultViz}"/>
</bean>   

<!-- declare a Spring bean to use the Camel Properties component in Spring XML -->
<bean id="properties"
      class="org.apache.camel.component.properties.PropertiesComponent">
    <property name="location" value="classpath:config.properties"/>
</bean>

<!-- bridge spring property placeholder with Camel -->
<!-- you must NOT use the <context:property-placeholder at the same time, only this bridge bean -->
<bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
  <property name="location" value="classpath:config.properties"/>
</bean>  

<bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
  <property name="configuration" ref="jmsConfig" />
</bean>

<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
  <property name="connectionFactory" ref="jmsConnectionFactory" />
  <property name="transacted" value="false" />
  <property name="concurrentConsumers" value="1" />
</bean>

<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
       <property name="brokerURL" value="vm://localhost" />
       <property name="redeliveryPolicy" ref="redeliveryPolicy" />
       <property name="prefetchPolicy" ref="prefetchPolicy" />
     </bean>

     <bean id="prefetchPolicy" class="org.apache.activemq.ActiveMQPrefetchPolicy">
       <property name="queuePrefetch" value="5" />
     </bean>

     <bean id="redeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy">
       <property name="maximumRedeliveries" value="1" />
       <property name="backOffMultiplier" value="2" />
       <property name="initialRedeliveryDelay" value="2000" />
       <property name="useExponentialBackOff" value="true" />
     </bean>

</beans>

我一直在尝试在applicationContext.xml中添加以下bean,将我的publish.properties文件中的六个字段注入到RandomEventGenerator类中的setter / getters中:

<!--  pass in publish.properties to RandomEventGenerator -->
<bean class="com.data.world2.RandomEventGenerator">
    <property name="makePub" value="${publish.makePub}"/>
    <property name="modelPub" value="${publish.modelPub}"/>
    <property name="serialNumberPub" value="${publish.serialNumberPub}"/>
    <property name="firmwareRevPub" value="${publish.firmwareRevPub}"/>
    <property name="sensorTypePub" value="${publish.sensorTypePub}"/>
    <property name="payloadPub" value="${publish.payloadPub}"/>
</bean> 

以及“properties”bean id的变体:

    <!-- declare a Spring bean to use the Camel Properties component in Spring XML -->
<bean id="publishProperties"
      class="org.apache.camel.component.properties.PropertiesComponent">
    <property name="location" value="classpath:publish.properties"/>
</bean>    

我还尝试将两个属性文件组合在我的属性bean ID中,如下所示:

    <bean id="properties"
      class="org.apache.camel.component.properties.PropertiesComponent">
    <property name="locations" value="classpath:config.properties,classpath:publish.properties"/>
</bean>

但我仍然收到以下错误:

Could not resolve placeholder 'publish.makePub' in string value "${publish.makePub}"

但我确实记得我必须在bridgePropertyPlaceholder中添加以使config.properties能够在Spring中运行。有没有办法可以为config.properties和publish.properties共享这个桥?或者有更简单的方法吗?

根据Frederic的反馈更新

这是我更新的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>

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

    <bean
            class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
    <context:component-scan base-package="com.data.world2" />
    <context:annotation-config />

    <camel:camelContext id="HelloWorldContext">

        <!-- Add Jackson library to render Java Map into JSON -->
        <camel:dataFormats>
          <camel:json id="jack" library="Jackson"/>
        </camel:dataFormats>

        <camel:route>
            <!-- sends {{config.numSamples}} request(s) to the hello world JMS queue every {{config.timeout}} seconds -->

            <camel:from
                uri="timer://hello.world.request.timer?fixedRate=true&amp;period={{config.timeout}}&amp;repeatCount={{config.numSamples}}"/>
            <camel:to uri="log:hello.world.request?level=INFO&amp;showAll=true" />
            <camel:bean ref="helloWorld" />

            <!-- now print out the map in JSON format -->
            <camel:marshal ref ="jack"/>
            <camel:convertBodyTo type="java.lang.String" />
            <camel:log message="${body}"/> 

            <!-- print out message that we are returning sensor event in JSON -->
            <camel:log message="Returned Random Sensor Event in JSON"/>

            <!-- print out values read from config.properties file -->
            <camel:log message="printing values read from config.properties file"/>
            <camel:log message="   config.timeout= {{config.timeout}} milliseconds"/> 
            <camel:log message="   config.numSamples= {{config.numSamples}} random Sensor Event(s)  ## NOTE: 0 or -1 means generate forever ##"/>
            <camel:log message="   config.defaultViz= {{config.defaultViz}}"/>

            <!-- now log the message -->
            <camel:to uri="log:hello.world.response?level=INFO&amp;showAll=true" />

            <!-- now send the message to the JMS queue -->
            <camel:to uri="jms:queue:helloworld.response" />

        </camel:route>

    </camel:camelContext>

    <!-- creates a java.util.Properties instance with values loaded from the supplied location -->
    <util:properties id="sensorProperties" location="classpath:/sensor.properties"/>

        <!--  pass in sensor.properties and defaultViz from config.properties -->
    <bean class="com.data.world2.sensor.SensorEventStore">
        <property name="sourceProperties" ref="sensorProperties" />
        <property name="defaultViz" value="${config.defaultViz}"/>
    </bean>

        <!--  pass in publish.properties to RandomEventGenerator -->
    <bean class="com.data.world2.RandomEventGenerator">
        <property name="makePub" value="${publish.makePub}"/>
        <property name="modelPub" value="${publish.modelPub}"/>
        <property name="serialNumberPub" value="${publish.serialNumberPub}"/>
        <property name="firmwareRevPub" value="${publish.firmwareRevPub}"/>
        <property name="sensorTypePub" value="${publish.sensorTypePub}"/>
        <property name="payloadPub" value="${publish.payloadPub}"/>
    </bean> 

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="ignoreResourceNotFound" value="false"/>
      <property name="locations">
        <list>
            <value>classpath:config.properties</value>
            <value>classpath:publish.properties</value>
        </list>
      </property>
    </bean>

    <!-- bridge spring property placeholder with Camel -->
    <!-- you must NOT use the <context:property-placeholder at the same time, only this bridge bean -->
    <bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
      <property name="location" value="classpath:config.properties"/>
    </bean> 

    <bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
      <property name="configuration" ref="jmsConfig" />
    </bean>

    <bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
      <property name="connectionFactory" ref="jmsConnectionFactory" />
      <property name="transacted" value="false" />
      <property name="concurrentConsumers" value="1" />
    </bean>

    <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
       <property name="brokerURL" value="vm://localhost" />
       <property name="redeliveryPolicy" ref="redeliveryPolicy" />
       <property name="prefetchPolicy" ref="prefetchPolicy" />
     </bean>

     <bean id="prefetchPolicy" class="org.apache.activemq.ActiveMQPrefetchPolicy">
       <property name="queuePrefetch" value="5" />
     </bean>

     <bean id="redeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy">
       <property name="maximumRedeliveries" value="1" />
       <property name="backOffMultiplier" value="2" />
       <property name="initialRedeliveryDelay" value="2000" />
       <property name="useExponentialBackOff" value="true" />
     </bean>

</beans>

这是我正在使用的publish.properties文件:

publish.makePub=true
publish.modelPub=true
publish.serialNumberPub=true
publish.firmwareRevPub=true
publish.sensorTypePub=false
publish.payloadPub=true 

以下是我的RandomEventGenerator类中的String Getters和Setters:

    // getters and setters
public String getMakePub() {
    return makePub;
}

public void setMakePub(String makePub) {
    this.makePub = makePub;
}

public String getModelPub() {
    return modelPub;
}

public void setModelPub(String modelPub) {
    this.modelPub = modelPub;
}

public String getSerialNumberPub() {
    return serialNumberPub;
}

public void setSerialNumberPub(String serialNumberPub) {
    this.serialNumberPub = serialNumberPub;
}

public String getFirmwareRevPub() {
    return firmwareRevPub;
}

public void setFirmwareRevPub(String firmwareRevPub) {
    this.firmwareRevPub = firmwareRevPub;
}

public String getSensorTypePub() {
    return sensorTypePub;
}

public void setSensorTypePub(String sensorTypePub) {
    this.sensorTypePub = sensorTypePub;
}

public String getPayloadPub() {
    return payloadPub;
}

public void setPayloadPub(String payloadPub) {
    this.payloadPub = payloadPub;
}

根据Claus的反馈更新了application.xml {和试错: - )}

<?xml version="1.0" encoding="UTF-8"?>

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

    <bean
        class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
    <context:component-scan base-package="com.data.world2" />
    <context:annotation-config />

    <camel:camelContext id="HelloWorldContext">

        <!-- Add Jackson library to render Java Map into JSON -->
        <camel:dataFormats>
          <camel:json id="jack" library="Jackson"/>
        </camel:dataFormats>

        <camel:route>
            <!-- sends {{config.numSamples}} request(s) to the hello world JMS queue every {{config.timeout}} seconds -->

            <camel:from
                uri="timer://hello.world.request.timer?fixedRate=true&amp;period={{config.timeout}}&amp;repeatCount={{config.numSamples}}"/>
            <camel:to uri="log:hello.world.request?level=INFO&amp;showAll=true" />
            <camel:bean ref="helloWorld" />

            <!-- now print out the ArrayList in JSON format -->
            <camel:marshal ref ="jack"/>
            <camel:convertBodyTo type="java.lang.String" />
            <camel:log message="${body}"/>

            <!-- print out message that we are returning sensor event in JSON -->
            <camel:log message="Returned Random Sensor Event in JSON"/>

            <!-- print out values read from config.properties file -->
            <camel:log message="printing values read from config.properties file"/>
            <camel:log message="   config.timeout= {{config.timeout}} milliseconds"/>
            <camel:log message="   config.numSamples= {{config.numSamples}} random Sensor Event(s)  ## NOTE: 0 or -1 means generate forever ##"/>
            <camel:log message="   config.defaultViz= {{config.defaultViz}}"/>

            <!-- now log the message -->
            <camel:to uri="log:hello.world.response?level=INFO&amp;showAll=true" />

            <!-- now send the message to the JMS queue -->
            <!--            <camel:to uri="jms:queue:helloworld.response" /> -->

        </camel:route>

    </camel:camelContext>

    <!-- creates a java.util.Properties instance with values loaded from the supplied location -->
    <util:properties id="sensorProperties" location="classpath:/sensor.properties"/>

    <!--  pass in sensor.properties and defaultViz from config.properties -->
    <bean class="com.data.world2.sensor.SensorEventStore">
        <property name="sourceProperties" ref="sensorProperties" />
        <property name="defaultViz" value="${config.defaultViz}"/>
    </bean>

    <!-- creates a java.util.Properties instance with values loaded from the supplied location -->
    <util:properties id="admissionProperties" location="classpath:/admission.properties"/>

    <!--  pass in admission.properties and defaultViz from config.properties -->
    <bean class="com.data.world2.admission.AdmissionEventStore">
        <property name="sourceAdmissionProperties" ref="admissionProperties" />
        <property name="defaultViz" value="${config.defaultViz}"/>
    </bean>

    <bean id="properties"
          class="org.apache.camel.component.properties.PropertiesComponent">
        <property name="locations">
          <list>
              <value>classpath:config.properties</value>
              <value>classpath:publish.properties</value>
              <value>classpath:admission.properties</value>
          </list>
        </property>
    </bean>

    <!-- bridge spring property placeholder with Camel -->
    <!-- you must NOT use the <context:property-placeholder at the same time, only this bridge bean -->
    <bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
      <property name="locations">
          <list>
              <value>classpath:config.properties</value>
              <value>classpath:publish.properties</value>
              <value>classpath:admission.properties</value>
          </list>
      </property>
    </bean>

    <!-- pass in publish.properties to RandomEventGenerator -->
    <bean class="com.data.world2.RandomEventGenerator">
        <property name="makePub" value="${publish.makePub}"/>
        <property name="modelPub" value="${publish.modelPub}"/>
        <property name="serialNumberPub" value="${publish.serialNumberPub}"/>
        <property name="firmwareRevPub" value="${publish.firmwareRevPub}"/>
        <property name="sensorTypePub" value="${publish.sensorTypePub}"/>
        <property name="payloadPub" value="${publish.payloadPub}"/>
    </bean>

    <!--     <bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent"> -->
    <!--       <property name="configuration" ref="jmsConfig" /> -->
    <!--     </bean> -->

    <!--     <bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration"> -->
    <!--       <property name="connectionFactory" ref="jmsConnectionFactory" /> -->
    <!--       <property name="transacted" value="false" /> -->
    <!--       <property name="concurrentConsumers" value="1" /> -->
    <!--     </bean> -->

    <!--     <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> -->
    <!--       <property name="brokerURL" value="vm://localhost" /> -->
    <!--       <property name="redeliveryPolicy" ref="redeliveryPolicy" /> -->
    <!--       <property name="prefetchPolicy" ref="prefetchPolicy" /> -->
    <!--     </bean> -->

    <!--     <bean id="prefetchPolicy" class="org.apache.activemq.ActiveMQPrefetchPolicy"> -->
    <!--       <property name="queuePrefetch" value="5" /> -->
    <!--    </bean> -->

    <!--     <bean id="redeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy"> -->
    <!--       <property name="maximumRedeliveries" value="1" /> -->
    <!--       <property name="backOffMultiplier" value="2" /> -->
    <!--       <property name="initialRedeliveryDelay" value="2000" /> -->
    <!--       <property name="useExponentialBackOff" value="true" /> -->
    <!--     </bean> -->

  </beans>  

最终修复

最后一个问题是我没有单独的@Autowired注入两个类。一旦我改变了代码:

    @Autowired
    SensorEventStore sensorEventStore;
    AdmissionEventStore admissionEventStore;

到此:

    @Autowired
    SensorEventStore sensorEventStore;
    @Autowired
    AdmissionEventStore admissionEventStore;

一切都正确注入。

3 个答案:

答案 0 :(得分:4)

我认为您可以在Camel BridgePropertyPlaceholderConfigurer

中定义多个位置
<bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
  <property name="location" value="classpath:config.properties,classpath:sensor.properties"/>
</bean> 

答案 1 :(得分:3)

你可以尝试这样的事情:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="ignoreResourceNotFound" value="false"/>
      <property name="locations">
        <list>
            <value>classpath:file1.properties</value>
            <value>classpath:file2.properties</value>
            <value>classpath:file3.properties</value>
        </list>
    </property>
</bean>

答案 2 :(得分:1)

我迟到了。这是我的样本。享受你的工作。

 <bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
    <property name="locations">
       <list>
          <value>classpath:config.properties</value>
          <value>classpath:sensor.properties</value>
       </list>
    </property>
</bean>