2013-10-24 32 views
2

所以我承認我在applicationContext.xml中有很多事情要做,但我不知道爲什麼我無法添加另一個名爲publish.properties的屬性文件,並使用它以類似的方式,我現在有一個config.properties。我也有一個sensor.properties,但它的一組keyvalue對(並且在任何情況下它們都可以正常加載)。使用Camel加載多個屬性文件

我一直在閱讀駱駝屬性文檔,即http://camel.apache.org/properties.html但它仍然不清楚我如何指定多個屬性文件,以便駱駝可以解決它們。

這是我的一個運行電流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添加到注入從我publish.properties六大領域文件到制定者/干將在我RandomEventGenerator類:

的「屬性」豆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>  

我也試過這兩個屬性文件結合自己的特性豆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 

這裏是字符串Getter and Setter在我的RandomEventGenerator類中:

// 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; 
} 

更新每一次反饋的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; 

一切正常注入。

+0

爲什麼選擇投票?因爲直到昨天晚些時候我才發現這個問題? – erj2code

回答

4

我認爲你可以在駱駝BridgePropertyPlaceholderConfigurer

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

嗨,克勞斯,我嘗試了你的建議,但我得到以下錯誤:異常在線程「主」「org.springframework.beans.factory.BeanInitializationException:無法加載屬性;嵌套異常是java.io.FileNotFoundException:類路徑資源[config.properties,classpath:sensor.properties]無法打開,因爲它不存在 – erj2code

+1

嘗試使用位置,例如以s結尾 –

+0

我嘗試使用bridgePropertyPlaceholder屬性文件列表,現在我沒有得到駱駝或春天的錯誤。我添加了更新的applicationContext.xml到我的問題結束。但是,我仍然看不到我的publish.properties注入。當我嘗試向我的HelloWorld類中添加一個@Autowired來爲AdmissionEventStore類加載admission.properties時,我嘗試拉動這些屬性時出現錯誤。在我的applicationContext.xml中是否還有其他需要的東西? – erj2code

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> 
+0

我按照你的建議修改了我的applicationContext.xml,但是當我檢查我的RandomEventGenerator類中的變量時,它們是空的。這是我更新的applicationContext.xml: – erj2code

+0

其實我發佈在我的問題的底部。下面是我的控制檯上看到的空值:'makePub = NULL modelPub = NULL serialNumberPub = NULL firmwareRevPub = NULL sensorTypePub = NULL payloadPub = null' – erj2code

+0

是你publish.properties你的JAR /戰爭的根源在哪裏?做它cointains folloging鍵publish.makePub publish.modelPub publish.serialNumberPub publish.firmwareRevPub publish.sensorTypePub publish.payloadPu –

1

我late.This是我的樣本定義多個位置。享受你的工作。

<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> 
相關問題