Java 如何将 Camel 属性加载到 Bean 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19457818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I load a Camel Property into a Bean?
提问by erj2code
I have been reading the following page on Camel properties: http://camel.apache.org/using-propertyplaceholder.htmland also reading the book "Camel In Action".
我一直在阅读有关 Camel 属性的以下页面:http: //camel.apache.org/using-propertyplaceholder.html并阅读“Camel In Action”一书。
I found Chapter 6 of "Camel In Action" very helpful in defining Camel properties, and I can load the following three properties from my config.properties:
我发现“Camel In Action”的第 6 章对定义 Camel 属性非常有帮助,我可以从我的 config.properties 加载以下三个属性:
config.timeout=10000
config.numSamples=1000
config.defaultViz=a
When I run my Java code I'm able to see the following three values inside my camel route in my applicationContext.xml, as shown in the thread#0 messages below:
当我运行 Java 代码时,我可以在 applicationContext.xml 中的骆驼路由中看到以下三个值,如下面的 thread#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
However, when I try to pass the variable {{config.defaultViz}} to a String called defaultViz in my SensorGenerator Java class, and print that string I get "{{config.defaultViz}}" on the console instead of the value contained within {{config.defaultViz}}.
但是,当我尝试将变量 {{config.defaultViz}} 传递给我的 SensorGenerator Java 类中名为 defaultViz 的字符串,并打印该字符串时,我在控制台上得到“{{config.defaultViz}}”而不是包含的值在 {{config.defaultViz}} 中。
In other words, here's what I see on the screen:
换句话说,这是我在屏幕上看到的:
Returning List
defaultViz= {{config.defaultViz}}
But I really want to see this on the screen:
但我真的很想在屏幕上看到这个:
Returning List
defaultViz=a
So what am I doing wrong in my applicationContext.xml?
那么我在 applicationContext.xml 中做错了什么?
UPDATED: The issue was that I needed to add a Bridge between Spring and Camel as outlined in the link I referenced above.
更新:问题是我需要在 Spring 和 Camel 之间添加一个桥梁,如我上面引用的链接中所述。
Here's my UPDATED applicationContext.xml with the bridge:
这是我更新的 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 Hymanson library to render Java Map into JSON -->
<camel:dataFormats>
<camel:json id="Hyman" library="Hymanson"/>
</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&period={{config.timeout}}" />
<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 ="Hyman"/>
<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&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>
I found this question that is similar but not quite the same: Injecting property into bean
我发现这个问题相似但不完全相同:Injecting property into bean
采纳答案by hveiga
The {{}}
notation just works inside the routes (ie inside the XML camel contexts). To use it in the bean I think you need to define the property placeholder bridge that camel provides but in your bean use the ${}
notation. The explanation of how to use that bridge is in the link you have provided.
该{{}}
表示法仅在路由内起作用(即在 XML 骆驼上下文内)。要在 bean 中使用它,我认为您需要定义骆驼提供的属性占位符桥,但在您的 bean 中使用${}
符号。如何使用该桥接器的说明在您提供的链接中。