Java – how do I load the camel attribute into a bean?

I've been reading the following pages about camel properties: http://camel.apache.org/using-propertyplaceholder.html I also read the book "camel in action"

I find chapter 6 of "camel in action" very useful in defining camel attributes. I can start from config Properties loads the following three properties:

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

When I run my java code, I can The following three values can be seen in the camel route in XML, as shown in the following thread #0 message:

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 the string named defaultviz in my sensorgenerator Java class and print the string, I get "{{config. Defaultviz}}" on the console instead of the included value in {{config. Defaultviz}}

In other words, this is 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 I'm in ApplicationContext What did you do wrong in XML?

Update: the problem is that I need to add a bridge between spring and camel, as described in the link referenced above

This is my updated ApplicationContext XML and bridges:

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

I found that the problem is similar but not identical: injecting property into bean

Solution

The {}} notation applies only to routing (that is, in the context of XML camel) To use it in beans, I think you need to define the property placeholder bridge provided by camel, but use the ${} notation in beans Instructions on how to use the bridge are located in the link you provide

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>