2

所以我承认我的 applicationContext.xml 中发生了很多事情,但我不确定为什么我不能添加另一个名为 publish.properties 的属性文件,并以与 config.properties 类似的方式使用它我现在有了。我也有一个 sensor.properties 但它是一组键值对(无论如何它们也加载得很好)。

我一直在阅读 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/getter 中:

<!--  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 的变体:

    <!-- 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 共享这个桥?或者有没有更简单的方法来做到这一点?

根据弗雷德里克的反馈更新

这是我更新的 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 类中的字符串 Getter 和 Setter:

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

一切都正确注入。

4

3 回答 3

4

I think you can define multiple locations in the Camel BridgePropertyPlaceholderConfigurer

<bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
  <property name="location" value="classpath:config.properties,classpath:sensor.properties"/>
</bean> 
于 2013-10-28T07:33:59.340 回答
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>
于 2013-10-25T16:36:31.673 回答
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> 
于 2016-09-14T03:26:45.333 回答