所以我承認我在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&period={{config.timeout}}&repeatCount={{config.numSamples}}"/>
<camel:to uri="log:hello.world.request?level=INFO&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&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&period={{config.timeout}}&repeatCount={{config.numSamples}}"/>
<camel:to uri="log:hello.world.request?level=INFO&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&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&period={{config.timeout}}&repeatCount={{config.numSamples}}"/>
<camel:to uri="log:hello.world.request?level=INFO&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&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;
一切正常注入。
爲什麼選擇投票?因爲直到昨天晚些時候我才發現這個問題? – erj2code