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 ファイルから 6 つのフィールドを RandomEventGenerator クラスのセッター/ゲッターに挿入しようとしています。

<!--  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}"

しかし、Spring から config.properties を機能させるには、bridgePropertyPlaceholder を追加する必要があったことを覚えています。このブリッジを 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 クラスの文字列ゲッターとセッターです:

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

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 に答える