camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Using PropertyPlaceholder
Date Sat, 11 May 2013 10:25:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Using+PropertyPlaceholder">Using PropertyPlaceholder</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >*Available as of Camel 2.7* <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{info} <br>If you use OSGi Blueprint then this only works from *2.11.1* or *2.10.5* onwards. <br>{info} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >Previously it was only the {{xs:string}} type attributes in the XML DSL that support placeholders. For example often a timeout attribute would be a {{xs:int}} type and thus you cannot set a string value as the placeholder key. This is now possible from Camel 2.7 onwards using a special placeholder namespace. <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="UsingPropertyPlaceholder-UsingPropertyPlaceholder"></a>Using PropertyPlaceholder</h2>
<p><b>Available as of Camel 2.3</b></p>

<p>Camel now provides a new <tt>PropertiesComponent</tt> in <b>camel-core</b> which allows you to use property placeholders when defining Camel <a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a> URIs. <br/>
This works much like you would do if using Spring's <tt>&lt;property-placeholder&gt;</tt> tag. However Spring have a limitation which prevents 3rd party frameworks to leverage Spring property placeholders to the fullest. See more at <a href="/confluence/display/CAMEL/How+do+I+use+Spring+Property+Placeholder+with+Camel+XML" title="How do I use Spring Property Placeholder with Camel XML">How do I use Spring Property Placeholder with Camel XML</a>.</p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Bridging Spring and Camel property placeholders</b><br />From Camel 2.10 onwards, you can bridge the Spring property placeholder with Camel, see further below for more details.</td></tr></table></div>

<p>The property placeholder is generally in use when doing:</p>
<ul class="alternate" type="square">
	<li>lookup or creating endpoints</li>
	<li>lookup of beans in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a></li>
	<li>additional supported in Spring XML (see below in examples)</li>
	<li>using Blueprint PropertyPlaceholder with Camel <a href="/confluence/display/CAMEL/Properties" title="Properties">Properties</a> component</li>
</ul>


<h3><a name="UsingPropertyPlaceholder-Syntax"></a>Syntax</h3>
<p>The syntax to use Camel's property placeholder is to use {{<tt>key</tt>}} for example {{<tt>file.uri</tt>}} where <tt>file.uri</tt> is the property key.<br/>
You can use property placeholders in parts of the endpoint URI's which for example you can use placeholders for parameters in the URIs.</p>

<h3><a name="UsingPropertyPlaceholder-PropertyResolver"></a>PropertyResolver</h3>
<p>Camel provides a pluggable mechanism which allows 3rd part to provide their own resolver to lookup properties. Camel provides a default implementation <tt>org.apache.camel.component.properties.DefaultPropertiesResolver</tt> which is capable of loading properties from the file system, classpath or <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>. You can prefix the locations with either:</p>
<ul class="alternate" type="square">
	<li><tt>ref:</tt> <b>Camel 2.4:</b> to lookup in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a></li>
	<li><tt>file:</tt> to load the from file system</li>
	<li><tt>classpath:</tt> to load from classpath (this is also the default if no prefix is provided)</li>
	<li><tt>blueprint:</tt> <b>Camel 2.7:</b> to use a specific OSGi blueprint placeholder service</li>
</ul>


<h3><a name="UsingPropertyPlaceholder-Defininglocation"></a>Defining location</h3>
<p>The <tt>PropertiesResolver</tt> need to know a location(s) where to resolve the properties. You can define 1 to many locations. If you define the location in a single String property you can separate multiple locations with comma such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
pc.setLocation(<span class="code-quote">"com/mycompany/myprop.properties,com/mycompany/other.properties"</span>);
</pre>
</div></div>

<h4><a name="UsingPropertyPlaceholder-Usingsystemandenvironmentvariablesinlocations"></a>Using system and environment variables in locations</h4>
<p><b>Available as of Camel 2.7</b></p>

<p>The location now supports using placeholders for JVM system properties and OS environments variables.</p>

<p>For example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
location=file:${karaf.home}/etc/foo.properties
</pre>
</div></div>

<p>In the location above we defined a location using the file scheme using the JVM system property with key <tt>karaf.home</tt>.</p>

<p>To use an OS environment variable instead you would have to prefix with env:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
location=file:${env:APP_HOME}/etc/foo.properties
</pre>
</div></div>
<p>Where <tt>APP_HOME</tt> is an OS environment.</p>

<p>You can have multiple placeholders in the same location, such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
location=file:${env:APP_HOME}/etc/${prop.name}.properties
</pre>
</div></div>


<h3><a name="UsingPropertyPlaceholder-ConfiguringinJavaDSL"></a>Configuring in Java DSL</h3>
<p>You have to create and register the <tt>PropertiesComponent</tt> under the name <tt>properties</tt> such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
PropertiesComponent pc = <span class="code-keyword">new</span> PropertiesComponent();
pc.setLocation(<span class="code-quote">"classpath:com/mycompany/myprop.properties"</span>);
context.addComponent(<span class="code-quote">"properties"</span>, pc);
</pre>
</div></div>

<h3><a name="UsingPropertyPlaceholder-ConfiguringinSpringXML"></a>Configuring in Spring XML</h3>
<p>Spring XML offers two variations to configure. You can define a spring bean as a <tt>PropertiesComponent</tt> which resembles the way done in Java DSL. Or you can use the <tt>&lt;propertyPlaceholder&gt;</tt> tag.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"properties"</span> class=<span class="code-quote">"org.apache.camel.component.properties.PropertiesComponent"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"location"</span> value=<span class="code-quote">"classpath:com/mycompany/myprop.properties"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>Using the <tt>&lt;propertyPlaceholder&gt;</tt> tag makes the configuration a bit more fresh such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;camelContext ...&gt;</span>
   <span class="code-tag">&lt;propertyPlaceholder id=<span class="code-quote">"properties"</span> location=<span class="code-quote">"com/mycompany/myprop.properties"</span>/&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Specifying the cache option inside XML</b><br />Camel 2.10 onwards supports specifying a value for the cache option both inside the Spring as well as the Blueprint XML.</td></tr></table></div>

<h3><a name="UsingPropertyPlaceholder-UsingaPropertiesfromtheRegistry"></a>Using a Properties from the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a></h3>
<p><b>Available as of Camel 2.4</b><br/>
For example in OSGi you may want to expose a service which returns the properties as a <tt>java.util.Properties</tt> object.</p>

<p>Then you could setup the <a href="/confluence/display/CAMEL/Properties" title="Properties">Properties</a> component as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
   <span class="code-tag">&lt;propertyPlaceholder id=<span class="code-quote">"properties"</span> location=<span class="code-quote">"ref:myProperties"</span>/&gt;</span>
</pre>
</div></div>
<p>Where <tt>myProperties</tt> is the id to use for lookup in the OSGi registry. Notice we use the <tt>ref:</tt> prefix to tell Camel that it should lookup the properties for the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>.</p>

<h3><a name="UsingPropertyPlaceholder-Examplesusingpropertiescomponent"></a>Examples using properties component</h3>
<p>When using property placeholders in the endpoint URIs you can either use the <tt>properties:</tt> component or define the placeholders directly in the URI. We will show example of both cases, starting with the former.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// properties
</span>cool.end=mock:result

<span class="code-comment">// route
</span>from(<span class="code-quote">"direct:start"</span>).to(<span class="code-quote">"properties:{{cool.end}}"</span>);
</pre>
</div></div>

<p>You can also use placeholders as a part of the endpoint uri:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// properties
</span>cool.foo=result

<span class="code-comment">// route
</span>from(<span class="code-quote">"direct:start"</span>).to(<span class="code-quote">"properties:mock:{{cool.foo}}"</span>);
</pre>
</div></div>
<p>In the example above the to endpoint will be resolved to <tt>mock:result</tt>.</p>

<p>You can also have properties with refer to each other such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// properties
</span>cool.foo=result
cool.concat=mock:{{cool.foo}}

<span class="code-comment">// route
</span>from(<span class="code-quote">"direct:start"</span>).to(<span class="code-quote">"properties:mock:{{cool.concat}}"</span>);
</pre>
</div></div>
<p>Notice how <tt>cool.concat</tt> refer to another property.</p>

<p>The <tt>properties:</tt> component also offers you to override and provide a location in the given uri using the <tt>locations</tt> option:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   from(<span class="code-quote">"direct:start"</span>).to(<span class="code-quote">"properties:bar.end?locations=com/mycompany/bar.properties"</span>);
</pre>
</div></div>

<h3><a name="UsingPropertyPlaceholder-Examples"></a>Examples</h3>
<p>You can also use property placeholders directly in the endpoint uris without having to use <tt>properties:</tt>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// properties
</span>cool.foo=result

<span class="code-comment">// route
</span>from(<span class="code-quote">"direct:start"</span>).to(<span class="code-quote">"mock:{{cool.foo}}"</span>);
</pre>
</div></div>

<p>And you can use them in multiple wherever you want them:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// properties
</span>cool.start=direct:start
cool.showid=<span class="code-keyword">true</span>
cool.result=result

<span class="code-comment">// route
</span>from(<span class="code-quote">"{{cool.start}}"</span>)
    .to(<span class="code-quote">"log:{{cool.start}}?showBodyType=<span class="code-keyword">false</span>&amp;showExchangeId={{cool.showid}}"</span>)
    .to(<span class="code-quote">"mock:{{cool.result}}"</span>);
</pre>
</div></div>

<p>You can also your property placeholders when using <a href="/confluence/display/CAMEL/ProducerTemplate" title="ProducerTemplate">ProducerTemplate</a> for example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
template.sendBody(<span class="code-quote">"{{cool.start}}"</span>, <span class="code-quote">"Hello World"</span>);
</pre>
</div></div>

<h3><a name="UsingPropertyPlaceholder-ExamplewithSimplelanguage"></a>Example with <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a> language</h3>
<p>The <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a> language now also support using property placeholders, for example in the route below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// properties
</span>cheese.quote=Camel rocks

<span class="code-comment">// route
</span>from(<span class="code-quote">"direct:start"</span>)
    .transform().simple(<span class="code-quote">"Hi ${body} <span class="code-keyword">do</span> you think ${properties:cheese.quote}?"</span>);
</pre>
</div></div>

<p>You can also specify the location in the <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a> language for example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// bar.properties
</span>bar.quote=Beer tastes good

<span class="code-comment">// route
</span>from(<span class="code-quote">"direct:start"</span>)
    .transform().simple(<span class="code-quote">"Hi ${body}. ${properties:com/mycompany/bar.properties:bar.quote}."</span>);
</pre>
</div></div>


<h3><a name="UsingPropertyPlaceholder-AdditionalpropertyplaceholdersupportedinSpringXML"></a>Additional property placeholder supported in Spring XML</h3>
<p>The property placeholders is also supported in many of the Camel Spring XML tags such as <tt>&lt;package&gt;, &lt;packageScan&gt;, &lt;contextScan&gt;, &lt;jmxAgent&gt;, &lt;endpoint&gt;, &lt;routeBuilder&gt;, &lt;proxy&gt;</tt> and the others.</p>

<p>The example below has property placeholder in the &lt;jmxAgent&gt; tag:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;propertyPlaceholder id=<span class="code-quote">"properties"</span> location=<span class="code-quote">"org/apache/camel/spring/jmx.properties"</span>/&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- we can use propery placeholders when we define the JMX agent --&gt;</span></span>
    &lt;jmxAgent id=<span class="code-quote">"agent"</span> registryPort=<span class="code-quote">"{{myjmx.port}}"</span> disabled=<span class="code-quote">"{{myjmx.disabled}}"</span>
              usePlatformMBeanServer=<span class="code-quote">"{{myjmx.usePlatform}}"</span>
              createConnector=<span class="code-quote">"true"</span>
              statisticsLevel=<span class="code-quote">"RoutesOnly"</span>/&gt;

    <span class="code-tag">&lt;route id=<span class="code-quote">"foo"</span> autoStartup=<span class="code-quote">"false"</span>&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"seda:start"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>

<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>You can also define property placeholders in the various attributes on the &lt;camelContext&gt; tag such as <tt>trace</tt> as shown here:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext trace=<span class="code-quote">"{{foo.trace}}"</span> xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;propertyPlaceholder id=<span class="code-quote">"properties"</span> location=<span class="code-quote">"org/apache/camel/spring/processor/myprop.properties"</span>/&gt;</span>

    <span class="code-tag">&lt;template id=<span class="code-quote">"camelTemplate"</span> defaultEndpoint=<span class="code-quote">"{{foo.cool}}"</span>/&gt;</span>

    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
        <span class="code-tag">&lt;setHeader headerName=<span class="code-quote">"{{foo.header}}"</span>&gt;</span>
            <span class="code-tag">&lt;simple&gt;</span>${in.body} World!<span class="code-tag">&lt;/simple&gt;</span>
        <span class="code-tag">&lt;/setHeader&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<h3><a name="UsingPropertyPlaceholder-OverridingapropertysettingusingaJVMSystemProperty"></a>Overriding a property setting using a JVM System Property</h3>
<p><b>Available as of Camel 2.5</b><br/>
It is possible to override a property value at runtime using a JVM System property without the need to restart the application to pick up the change. This may also be accomplished from the command line by creating a JVM System property of the same name as the property it replaces with a new value. An example of this is given below</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
PropertiesComponent pc = context.getComponent(<span class="code-quote">"properties"</span>, PropertiesComponent.class);
pc.setCache(<span class="code-keyword">false</span>);
        
<span class="code-object">System</span>.setProperty(<span class="code-quote">"cool.end"</span>, <span class="code-quote">"mock:override"</span>);
<span class="code-object">System</span>.setProperty(<span class="code-quote">"cool.result"</span>, <span class="code-quote">"override"</span>);

context.addRoutes(<span class="code-keyword">new</span> RouteBuilder() {
    @Override
    <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span> Exception {
        from(<span class="code-quote">"direct:start"</span>).to(<span class="code-quote">"properties:cool.end"</span>);
        from(<span class="code-quote">"direct:foo"</span>).to(<span class="code-quote">"properties:mock:{{cool.result}}"</span>);
    }
});
context.start();

getMockEndpoint(<span class="code-quote">"mock:override"</span>).expectedMessageCount(2);

template.sendBody(<span class="code-quote">"direct:start"</span>, <span class="code-quote">"Hello World"</span>);
template.sendBody(<span class="code-quote">"direct:foo"</span>, <span class="code-quote">"Hello Foo"</span>);

<span class="code-object">System</span>.clearProperty(<span class="code-quote">"cool.end"</span>);
<span class="code-object">System</span>.clearProperty(<span class="code-quote">"cool.result"</span>);
        
assertMockEndpointsSatisfied();
</pre>
</div></div>  

<h3><a name="UsingPropertyPlaceholder-UsingpropertyplaceholdersforanykindofattributeintheXMLDSL"></a>Using property placeholders for any kind of attribute in the XML DSL</h3>
<p><b>Available as of Camel 2.7</b></p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td>If you use OSGi Blueprint then this only works from <b>2.11.1</b> or <b>2.10.5</b> onwards.</td></tr></table></div>

<p>Previously it was only the <tt>xs:string</tt> type attributes in the XML DSL that support placeholders. For example often a timeout attribute would be a <tt>xs:int</tt> type and thus you cannot set a string value as the placeholder key. This is now possible from Camel 2.7 onwards using a special placeholder namespace.</p>

<p>In the example below we use the <tt>prop</tt> prefix for the namespace <tt><a href="http://camel.apache.org/schema/placeholder" class="external-link" rel="nofollow">http://camel.apache.org/schema/placeholder</a></tt> by which we can use the <tt>prop</tt> prefix in the attributes in the XML DSLs. Notice how we use that in the <a href="/confluence/display/CAMEL/Multicast" title="Multicast">Multicast</a> to indicate that the option <tt>stopOnException</tt> should be the value of the placeholder with the key "stop".</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
       <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
       <span class="code-keyword">xmlns:prop</span>=<span class="code-quote">"http://camel.apache.org/schema/placeholder"</span>
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
    "&gt;

    <span class="code-tag"><span class="code-comment">&lt;!-- Notice in the declaration above, we have defined the prop prefix as the Camel placeholder namespace --&gt;</span></span>

    <span class="code-tag">&lt;bean id=<span class="code-quote">"damn"</span> class=<span class="code-quote">"java.lang.IllegalArgumentException"</span>&gt;</span>
        <span class="code-tag">&lt;constructor-arg index=<span class="code-quote">"0"</span> value=<span class="code-quote">"Damn"</span>/&gt;</span>
    <span class="code-tag">&lt;/bean&gt;</span>

    <span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>

        &lt;propertyPlaceholder id=<span class="code-quote">"properties"</span>
                             location=<span class="code-quote">"classpath:org/apache/camel/component/properties/myprop.properties"</span>
                             xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>/&gt;

        <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
            &lt;!-- use prop namespace, to define a property placeholder, which maps to
                 option stopOnException={{stop}} --&gt;
            <span class="code-tag">&lt;multicast prop:stopOnException=<span class="code-quote">"stop"</span>&gt;</span>
                <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:a"</span>/&gt;</span>
                <span class="code-tag">&lt;throwException ref=<span class="code-quote">"damn"</span>/&gt;</span>
                <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:b"</span>/&gt;</span>
            <span class="code-tag">&lt;/multicast&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>

    <span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<p>In our properties file we have the value defined as</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
stop=<span class="code-keyword">true</span>
</pre>
</div></div>

<h3><a name="UsingPropertyPlaceholder-UsingpropertyplaceholderintheJavaDSL"></a>Using property placeholder in the Java DSL</h3>
<p><b>Available as of Camel 2.7</b></p>

<p>Likewise we have added support for defining placeholders in the Java DSL using the new <tt>placeholder</tt> DSL as shown in the following equivalent example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// use a property placeholder <span class="code-keyword">for</span> the option stopOnException on the Multicast EIP
</span>    <span class="code-comment">// which should have the value of {{stop}} key being looked up in the properties file
</span>    .multicast().placeholder(<span class="code-quote">"stopOnException"</span>, <span class="code-quote">"stop"</span>)
        .to(<span class="code-quote">"mock:a"</span>).throwException(<span class="code-keyword">new</span> IllegalAccessException(<span class="code-quote">"Damn"</span>)).to(<span class="code-quote">"mock:b"</span>);
</pre>
</div></div>


<h3><a name="UsingPropertyPlaceholder-UsingBlueprintpropertyplaceholderwithCamelroutes"></a>Using Blueprint property placeholder with Camel routes</h3>
<p><b>Available as of Camel 2.7</b></p>

<p>Camel supports <a href="/confluence/display/CAMEL/Using+OSGi+blueprint+with+Camel" title="Using OSGi blueprint with Camel">Blueprint</a> which also offers a property placeholder service. Camel supports convention over configuration, so all you have to do is to define the OSGi Blueprint property placeholder in the XML file as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Using OSGi blueprint property placeholders in Camel routes</b></div><div class="codeContent panelContent">
<pre class="code-xml">&lt;blueprint xmlns=<span class="code-quote">"http://www.osgi.org/xmlns/blueprint/v1.0.0"</span>
           <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
           <span class="code-keyword">xmlns:cm</span>=<span class="code-quote">"http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"</span>
           xsi:schemaLocation="
           http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"&gt;

    <span class="code-tag"><span class="code-comment">&lt;!-- OSGI blueprint property placeholder --&gt;</span></span>
    <span class="code-tag">&lt;cm:property-placeholder id=<span class="code-quote">"myblueprint.placeholder"</span> persistent-id=<span class="code-quote">"camel.blueprint"</span>&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- list some properties for this test --&gt;</span></span>
        <span class="code-tag">&lt;cm:default-properties&gt;</span>
            <span class="code-tag">&lt;cm:property name=<span class="code-quote">"result"</span> value=<span class="code-quote">"mock:result"</span>/&gt;</span>
        <span class="code-tag">&lt;/cm:default-properties&gt;</span>
    <span class="code-tag">&lt;/cm:property-placeholder&gt;</span>

    <span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/blueprint"</span>&gt;</span>

        &lt;!-- in the route we can use {{ }} placeholders which will lookup in blueprint
             as Camel will auto detect the OSGi blueprint property placeholder and use it --&gt;
        <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:foo"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"{{result}}"</span>/&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>

    <span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;/blueprint&gt;</span>
</pre>
</div></div>

<p>By default Camel detects and uses OSGi blueprint property placeholder service. You can disable this by setting the attribute <tt>useBlueprintPropertyResolver</tt> to false on the <tt>&lt;camelContext&gt;</tt> definition.</p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>About placeholder syntaxes</b><br />Notice how we can use the Camel syntax for placeholders {{ }} in the Camel route, which will lookup the value from OSGi blueprint.<br/>
The blueprint syntax for placeholders is ${ }. So outside the &lt;camelContext&gt; you must use the ${ } syntax. Where as inside &lt;camelContext&gt; you must use {{ }} syntax.<br/>
OSGi blueprint allows you to configure the syntax, so you can actually align those if you want.</td></tr></table></div>

<p>You can also explicit refer to a specific OSGi blueprint property placeholder by its id. For that you need to use the Camel's &lt;propertyPlaceholder&gt; as shown in the example below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Explicit referring to a OSGi blueprint placeholder in Camel</b></div><div class="codeContent panelContent">
<pre class="code-xml">&lt;blueprint xmlns=<span class="code-quote">"http://www.osgi.org/xmlns/blueprint/v1.0.0"</span>
           <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
           <span class="code-keyword">xmlns:cm</span>=<span class="code-quote">"http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"</span>
           xsi:schemaLocation="
           http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"&gt;

    <span class="code-tag"><span class="code-comment">&lt;!-- OSGI blueprint property placeholder --&gt;</span></span>
    <span class="code-tag">&lt;cm:property-placeholder id=<span class="code-quote">"myblueprint.placeholder"</span> persistent-id=<span class="code-quote">"camel.blueprint"</span>&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- list some properties for this test --&gt;</span></span>
        <span class="code-tag">&lt;cm:default-properties&gt;</span>
            <span class="code-tag">&lt;cm:property name=<span class="code-quote">"prefix.result"</span> value=<span class="code-quote">"mock:result"</span>/&gt;</span>
        <span class="code-tag">&lt;/cm:default-properties&gt;</span>
    <span class="code-tag">&lt;/cm:property-placeholder&gt;</span>

    <span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/blueprint"</span>&gt;</span>

        <span class="code-tag"><span class="code-comment">&lt;!-- using Camel properties component and refer to the blueprint property placeholder by its id --&gt;</span></span>
        &lt;propertyPlaceholder id=<span class="code-quote">"properties"</span> location=<span class="code-quote">"blueprint:myblueprint.placeholder"</span>
                             prefixToken=<span class="code-quote">"[["</span> suffixToken=<span class="code-quote">"]]"</span>
                             propertyPrefix=<span class="code-quote">"prefix."</span>/&gt;

        <span class="code-tag"><span class="code-comment">&lt;!-- in the route we can use {{ }} placeholders which will lookup in blueprint --&gt;</span></span>
        <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:foo"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"[[result]]"</span>/&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>

    <span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;/blueprint&gt;</span>
</pre>
</div></div>

<p>Notice how we use the <tt>blueprint</tt> scheme to refer to the OSGi blueprint placeholder by its id. This allows you to mix and match, for example you can also have additional schemes in the location. For example to load a file from the classpath you can do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
location=<span class="code-quote">"blueprint:myblueprint.placeholder,classpath:myproperties.properties"</span>
</pre>
</div></div>

<p>Each location is separated by comma.</p>


<h4><a name="UsingPropertyPlaceholder-OverridingBlueprintpropertyplaceholdersoutsideCamelContext"></a>Overriding Blueprint property placeholders outside CamelContext</h4>
<p><b>Available as of Camel 2.10.4</b></p>

<p>When using Blueprint property placeholder in the Blueprint XML file, you can declare the properties directly in the XML file as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag"><span class="code-comment">&lt;!-- blueprint property placeholders --&gt;</span></span>
<span class="code-tag">&lt;cm:property-placeholder persistent-id=<span class="code-quote">"my-placeholders"</span>&gt;</span>
  <span class="code-tag">&lt;cm:default-properties&gt;</span>
    <span class="code-tag">&lt;cm:property name=<span class="code-quote">"greeting"</span> value=<span class="code-quote">"Hello"</span>/&gt;</span>
    <span class="code-tag">&lt;cm:property name=<span class="code-quote">"destination"</span> value=<span class="code-quote">"mock:result"</span>/&gt;</span>
  <span class="code-tag">&lt;/cm:default-properties&gt;</span>
<span class="code-tag">&lt;/cm:property-placeholder&gt;</span>

<span class="code-tag"><span class="code-comment">&lt;!-- a bean that uses a blueprint property placeholder --&gt;</span></span>
<span class="code-tag">&lt;bean id=<span class="code-quote">"myCoolBean"</span> class=<span class="code-quote">"org.apache.camel.test.blueprint.MyCoolBean"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"say"</span> value=<span class="code-quote">"${greeting}"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/blueprint"</span>&gt;</span>

  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
    <span class="code-tag">&lt;bean ref=<span class="code-quote">"myCoolBean"</span> method=<span class="code-quote">"saySomething"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"{{destination}}"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>

<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>Notice that we have a &lt;bean&gt; which refers to one of the properties. And in the Camel route we refer to the other using the {{ }} notation.</p>

<p>Now if you want to override these Blueprint properties from an unit test, you can do this as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">@Override
<span class="code-keyword">protected</span> <span class="code-object">String</span> useOverridePropertiesWithConfigAdmin(Dictionary props) {
    <span class="code-comment">// add the properties we want to override
</span>    props.put(<span class="code-quote">"greeting"</span>, <span class="code-quote">"Bye"</span>);

    <span class="code-comment">// <span class="code-keyword">return</span> the PID of the config-admin we are using in the blueprint xml file
</span>    <span class="code-keyword">return</span> <span class="code-quote">"my-placeholders"</span>;
}
</pre>
</div></div>

<p>To do this we override and implement the <tt>useOverridePropertiesWithConfigAdmin</tt> method. We can then put the properties we want to override on the given props parameter. And the return value <b>must</b> be the persistence-id of the &lt;cm:property-placeholder&gt; tag, which you define in the blueprint XML file.</p>

<h4><a name="UsingPropertyPlaceholder-Using.cfgor.propertiesfileforBlueprintpropertyplaceholders"></a>Using .cfg or .properties file for Blueprint property placeholders</h4>
<p><b>Available as of Camel 2.10.4</b></p>

<p>When using Blueprint property placeholder in the Blueprint XML file, you can declare the properties in a .properties or .cfg file. If you use Apache ServieMix / Karaf then this container has a convention that it loads the properties from a file in the etc directory with the naming etc/pid.cfg, where pid is the persistence-id.</p>

<p>For example in the blueprint XML file we have the persistence-id="stuff", which mean it will load the configuration file as etc/stuff.cfg.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag"><span class="code-comment">&lt;!-- blueprint property placeholders, that will use etc/stuff.cfg as the properties file --&gt;</span></span>
<span class="code-tag">&lt;cm:property-placeholder persistent-id=<span class="code-quote">"stuff"</span>/&gt;</span>

<span class="code-tag"><span class="code-comment">&lt;!-- a bean that uses a blueprint property placeholder --&gt;</span></span>
<span class="code-tag">&lt;bean id=<span class="code-quote">"myCoolBean"</span> class=<span class="code-quote">"org.apache.camel.test.blueprint.MyCoolBean"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"say"</span> value=<span class="code-quote">"${greeting}"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/blueprint"</span>&gt;</span>

  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
    <span class="code-tag">&lt;bean ref=<span class="code-quote">"myCoolBean"</span> method=<span class="code-quote">"saySomething"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>

<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>Now if you want to unit test this blueprint XML file, then you can override the <tt>loadConfigAdminConfigurationFile</tt> and tell Camel which file to load as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">@Override
<span class="code-keyword">protected</span> <span class="code-object">String</span>[] loadConfigAdminConfigurationFile() {
    <span class="code-comment">// <span class="code-object">String</span>[0] = tell Camel the path of the .cfg file to use <span class="code-keyword">for</span> OSGi ConfigAdmin in the blueprint XML file
</span>    <span class="code-comment">// <span class="code-object">String</span>[1] = tell Camel the persistence-id of the cm:property-placeholder in the blueprint XML file
</span>    <span class="code-keyword">return</span> <span class="code-keyword">new</span> <span class="code-object">String</span>[]{<span class="code-quote">"src/test/resources/etc/stuff.cfg"</span>, <span class="code-quote">"stuff"</span>};
}
</pre>
</div></div>

<p>Notice that this method requires to return a String[] with 2 values. The 1st value is the path for the configuration file to load.<br/>
The 2nd value is the persistence-id of the &lt;cm:property-placeholder&gt; tag.</p>

<p>The stuff.cfg file is just a plain properties file with the property placeholders such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
## <span class="code-keyword">this</span> is a comment
greeting=Bye
</pre>
</div></div>


<h4><a name="UsingPropertyPlaceholder-Using.cfgfileandoverridingpropertiesforBlueprintpropertyplaceholders"></a>Using .cfg file and overriding properties for Blueprint property placeholders</h4>

<p>You can do both as well. Here is a complete example. First we have the Blueprint XML file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">&lt;blueprint xmlns=<span class="code-quote">"http://www.osgi.org/xmlns/blueprint/v1.0.0"</span>
           <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
           <span class="code-keyword">xmlns:cm</span>=<span class="code-quote">"http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"</span>
           xsi:schemaLocation="
             http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.0.0.xsd
             http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"&gt;

  <span class="code-tag"><span class="code-comment">&lt;!-- blueprint property placeholders, that will use etc/stuff.cfg as the properties file --&gt;</span></span>
  <span class="code-tag">&lt;cm:property-placeholder persistent-id=<span class="code-quote">"stuff"</span>/&gt;</span>

  <span class="code-tag"><span class="code-comment">&lt;!-- a bean that uses a blueprint property placeholder --&gt;</span></span>
  <span class="code-tag">&lt;bean id=<span class="code-quote">"myCoolBean"</span> class=<span class="code-quote">"org.apache.camel.test.blueprint.MyCoolBean"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"say"</span> value=<span class="code-quote">"${greeting}"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"echo"</span> value=<span class="code-quote">"${echo}"</span>/&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>

  <span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/blueprint"</span>&gt;</span>

    <span class="code-tag">&lt;route&gt;</span>
      <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
      <span class="code-tag">&lt;bean ref=<span class="code-quote">"myCoolBean"</span> method=<span class="code-quote">"saySomething"</span>/&gt;</span>
      <span class="code-tag">&lt;to uri=<span class="code-quote">"{{destination}}"</span>/&gt;</span>
      <span class="code-tag">&lt;bean ref=<span class="code-quote">"myCoolBean"</span> method=<span class="code-quote">"echoSomething"</span>/&gt;</span>
      <span class="code-tag">&lt;to uri=<span class="code-quote">"{{destination}}"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>

  <span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;/blueprint&gt;</span>
</pre>
</div></div>

<p>And in the unit test class we do as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">/**
 * This example will load a Blueprint .cfdg file, and also override its property placeholders from <span class="code-keyword">this</span> unit test
 * source code directly.
 */
<span class="code-keyword">public</span> class ConfigAdminLoadConfigurationFileAndOverrideTest <span class="code-keyword">extends</span> CamelBlueprintTestSupport {

    @Override
    <span class="code-keyword">protected</span> <span class="code-object">String</span> getBlueprintDescriptor() {
        <span class="code-comment">// which blueprint XML file to use <span class="code-keyword">for</span> <span class="code-keyword">this</span> test
</span>        <span class="code-keyword">return</span> <span class="code-quote">"org/apache/camel/test/blueprint/configadmin-loadfileoverride.xml"</span>;
    }

    @Override
    <span class="code-keyword">protected</span> <span class="code-object">String</span>[] loadConfigAdminConfigurationFile() {
        <span class="code-comment">// which .cfg file to use, and the name of the persistence-id
</span>        <span class="code-keyword">return</span> <span class="code-keyword">new</span> <span class="code-object">String</span>[]{<span class="code-quote">"src/test/resources/etc/stuff.cfg"</span>, <span class="code-quote">"stuff"</span>};
    }

    @Override
    <span class="code-keyword">protected</span> <span class="code-object">String</span> useOverridePropertiesWithConfigAdmin(Dictionary props) <span class="code-keyword">throws</span> Exception {
        <span class="code-comment">// override / add extra properties
</span>        props.put(<span class="code-quote">"destination"</span>, <span class="code-quote">"mock:extra"</span>);

        <span class="code-comment">// <span class="code-keyword">return</span> the persistence-id to use
</span>        <span class="code-keyword">return</span> <span class="code-quote">"stuff"</span>;
    }

    @Test
    <span class="code-keyword">public</span> void testConfigAdmin() <span class="code-keyword">throws</span> Exception {
        <span class="code-comment">// regular unit test method
</span>        getMockEndpoint(<span class="code-quote">"mock:extra"</span>).expectedBodiesReceived(<span class="code-quote">"Bye World"</span>, <span class="code-quote">"Yay Bye WorldYay Bye World"</span>);

        template.sendBody(<span class="code-quote">"direct:start"</span>, <span class="code-quote">"World"</span>);

        assertMockEndpointsSatisfied();
    }

}
</pre>
</div></div>

<p>And the <tt>etc/stuff.cfg</tt> configuration file contains</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
greeting=Bye
echo=Yay
destination=mock:result
</pre>
</div></div>


<h3><a name="UsingPropertyPlaceholder-BridgingSpringandCamelpropertyplaceholders"></a>Bridging Spring and Camel property placeholders</h3>
<p><b>Available as of Camel 2.10</b></p>

<p>The Spring Framework does not allow 3rd party frameworks such as Apache Camel to seamless hook into the Spring property placeholder mechanism. However you can easily bridge Spring and Camel by declaring a Spring bean with the type <tt>org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer</tt>, which is a Spring <tt>org.springframework.beans.factory.config.PropertyPlaceholderConfigurer</tt> type. </p>

<p>To bridge Spring and Camel you must define a single bean as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Bridging Spring and Camel property placeholders</b></div><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag"><span class="code-comment">&lt;!-- bridge spring property placeholder with Camel --&gt;</span></span>
<span class="code-tag"><span class="code-comment">&lt;!-- you must NOT use the &lt;context:property-placeholder at the same time, only this bridge bean --&gt;</span></span>
<span class="code-tag">&lt;bean id=<span class="code-quote">"bridgePropertyPlaceholder"</span> class=<span class="code-quote">"org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"location"</span> value=<span class="code-quote">"classpath:org/apache/camel/component/properties/cheese.properties"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

</pre>
</div></div>

<p>You <b>must not</b> use the spring &lt;context:property-placeholder&gt; namespace at the same time; this is not possible.</p>

<p>After declaring this bean, you can define property placeholders using both the Spring style, and the Camel style within the &lt;camelContext&gt; tag as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Using bridge property placeholders</b></div><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag"><span class="code-comment">&lt;!-- a bean that uses Spring property placeholder --&gt;</span></span>
<span class="code-tag"><span class="code-comment">&lt;!-- the ${hi} is a spring property placeholder --&gt;</span></span>
<span class="code-tag">&lt;bean id=<span class="code-quote">"hello"</span> class=<span class="code-quote">"org.apache.camel.component.properties.HelloBean"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"greeting"</span> value=<span class="code-quote">"${hi}"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
  <span class="code-tag"><span class="code-comment">&lt;!-- in this route we use Camels property placeholder {{ }} style --&gt;</span></span>
  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:{{cool.bar}}"</span>/&gt;</span>
    <span class="code-tag">&lt;bean ref=<span class="code-quote">"hello"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"{{cool.end}}"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>

</pre>
</div></div>

<p>Notice how the hello bean is using pure Spring property placeholders using the ${ } notation. And in the Camel routes we use the Camel placeholder notation with {{ }}.</p>


<h3><a name="UsingPropertyPlaceholder-OverridingpropertiesfromCameltestkit"></a>Overriding properties from Camel test kit</h3>
<p><b>Available as of Camel 2.10</b></p>

<p>When <a href="/confluence/display/CAMEL/Testing" title="Testing">Testing</a> with Camel and using the <a href="/confluence/display/CAMEL/Properties" title="Properties">Properties</a> component, you may want to be able to provide the properties to be used from directly within the unit test source code.<br/>
This is now possible from Camel 2.10 onwards, as the Camel test kits, eg <tt>CamelTestSupport</tt> class offers the following methods</p>
<ul class="alternate" type="square">
	<li>useOverridePropertiesWithPropertiesComponent</li>
	<li>ignoreMissingLocationWithPropertiesComponent</li>
</ul>


<p>So for example in your unit test classes, you can override the <tt>useOverridePropertiesWithPropertiesComponent</tt> method and return a <tt>java.util.Properties</tt> that contains the properties which should be preferred to be used.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Providing properties from within unit test source</b></div><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// override <span class="code-keyword">this</span> method to provide our custom properties we use in <span class="code-keyword">this</span> unit test
</span>@Override
<span class="code-keyword">protected</span> Properties useOverridePropertiesWithPropertiesComponent() {
    Properties extra = <span class="code-keyword">new</span> Properties();
    extra.put(<span class="code-quote">"destination"</span>, <span class="code-quote">"mock:extra"</span>);
    extra.put(<span class="code-quote">"greeting"</span>, <span class="code-quote">"Bye"</span>);
    <span class="code-keyword">return</span> extra;
}
</pre>
</div></div>

<p>This can be done from any of the Camel Test kits, such as camel-test, camel-test-spring, and camel-test-blueprint.</p>

<p>The <tt>ignoreMissingLocationWithPropertiesComponent</tt> can be used to instruct Camel to ignore any locations which was not discoverable, for example if you run the unit test, in an environment that does not have access to the location of the properties. </p>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Using+PropertyPlaceholder">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=13271723&revisedVersion=22&originalVersion=21">View Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Using+PropertyPlaceholder?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message