2013-10-18 14 views
6

Ho letto la seguente pagina sulle proprietà Camel: http://camel.apache.org/using-propertyplaceholder.html e anche leggendo il libro "Camel In Action".Come caricare una proprietà Camel in un bean?

ho trovato il Capitolo 6 di "Cammello In Azione" molto utile nella definizione delle proprietà Camel, e posso caricare i seguenti tre oggetti di miei config.properties:

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

Quando eseguo il mio codice Java I' m in grado di vedere i seguenti tre valori dentro il mio percorso cammello nel mio applicationContext.xml, come mostrato nel thread # 0 messaggi qui sotto:

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 

Tuttavia, quando provo a passare la variabile {{} config.defaultViz } a una stringa denominata defaultViz nella mia classe Java SensorGenerator e prin t quella stringa ottengo "{{config.defaultViz}}" sulla console al posto del valore contenuto in {{config.defaultViz}}.

In altre parole, ecco quello che vedo sullo schermo:

Returning List 
defaultViz= {{config.defaultViz}} 

Ma voglio davvero vedere questo sullo schermo:

Returning List 
defaultViz=a 

Così che cosa sto facendo di sbagliato nel mio applicationContext .xml?

AGGIORNATO: Il problema era che avevo bisogno di aggiungere un ponte tra Spring e Camel come delineato nel collegamento a cui ho fatto riferimento sopra.

Ecco la mia applicationContext.xml AGGIORNATO con il ponte:

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

Ho trovato questa domanda che è simile ma non proprio la stessa cosa: Injecting property into bean

risposta

12

La notazione {{}} solo lavora all'interno dei percorsi (ad esempio all'interno i contesti cammello XML). Per utilizzarlo nel bean, penso che sia necessario definire il bridge segnaposto della proprietà fornito dal camel ma nel proprio bean utilizzare la notazione ${}. La spiegazione di come usare quel bridge è nel link che hai fornito.

+2

Grazie! Ha funzionato. Ho aggiunto il Bridge e ora ho il mio defaultViz. Aggiornerò applicationContext.xml a ciò che ora ho. Forse sarà utile a qualcun altro in futuro. :-) – erj2code

+0

È piuttosto strano che tutte le proprietà in etc/folder non siano visibili al contesto di primavera. Sembra che il contesto di primavera e il contesto del modello siano caricati indipendentemente. Anche il bean dichiarato nel Blueprint non è visibile nel contesto di primavera. sembra che bridge sia l'unico modo per il contesto di Spring, ovvero \t \t

Problemi correlati