2013-10-18 57 views
6

我一直在閱讀關於駱駝屬性的以下頁面:http://camel.apache.org/using-propertyplaceholder.html並且還閱讀了「駱駝在行動」一書。如何將駱駝屬性加載到Bean中?

我發現的第6章「駱駝在行動」非常有幫助的定義駱駝屬性,我可以從我config.properties加載以下三個屬性:

config.timeout=10000 
config.numSamples=1000 
config.defaultViz=a 

當我運行我的Java代碼我米能看到我的駱駝路線內的以下三個值在我的applicationContext.xml,如圖線程#低於0消息:

14669 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO route1 - printing values read from config.properties file 
14669 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO route1 - config.timeout= 10000 
14669 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO route1 - config.numSamples= 1000 
14670 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO route1 - config.defaultViz= a 

然而,當我嘗試通過可變{{config.defaultViz} }傳遞到SensorGenerator Java類中的一個名爲defaultViz的字符串,以及prin t我在控制檯上獲得「{{config.defaultViz}}」而不是{{config.defaultViz}}中包含的值。

換句話說,這裏就是我在屏幕上看到:

Returning List 
defaultViz= {{config.defaultViz}} 

但我真的希望看到這個屏幕上:

Returning List 
defaultViz=a 

所以我在做什麼錯在我的applicationContext .XML?

更新:問題是我需要在Spring和Camel之間添加一個橋,如上面引用的鏈接中所述。

這裏是我更新後的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 a request to the hello world JMS queue every 10 seconds --> 
      <camel:from 
       uri="timer://hello.world.request.timer?fixedRate=true&amp;period={{config.timeout}}" /> 
      <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 values read from config.properties file --> 
      <camel:log message="printing values read from config.properties file"/> 
      <camel:log message="config.timeout= {{config.timeout}}"/> 
      <camel:log message="config.numSamples= {{config.numSamples}}"/> 
      <camel:log message="config.defaultViz= {{config.defaultViz}}"/> 

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

     </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.SensorGenerator"> 
     <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> 

</beans> 

我發現這個問題是相似但不相同:Injecting property into bean

回答

12

{{}}符號只是工作路線內(即內XML駱駝上下文)。要在bean中使用它,我認爲您需要定義駱駝提供的屬性佔位符橋,但在bean中使用${}表示法。如何使用該橋的解釋在您提供的鏈接中。

+2

謝謝!這工作。我加了橋,現在我得到了我的defaultViz。我將更新applicationContext.xml到我現在擁有的。也許這對未來的其他人有用。 :-) – erj2code

+0

很奇怪,在etc /文件夾中的所有屬性在spring上下文中都是不可見的。看起來,Spring上下文和藍圖上下文是獨立加載的。即使在Blueprint中聲明的bean在春季環境中也是不可見的。看起來像橋只是Spring上下文的唯一方式,即 \t \t