This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • Using PropertyPlaceholder
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 20 Next »

Using PropertyPlaceholder

Available as of Camel 2.3

Camel now provides a new PropertiesComponent in camel-core which allows you to use property placeholders when defining Camel Endpoint URIs.
This works much like you would do if using Spring's <property-placeholder> tag. However Spring have a limitation which prevents 3rd party frameworks to leverage Spring property placeholders to the fullest. See more at How do I use Spring Property Placeholder with Camel XML.

Bridging Spring and Camel property placeholders

From Camel 2.10 onwards, you can bridge the Spring property placeholder with Camel, see further below for more details.

The property placeholder is generally in use when doing:

  • lookup or creating endpoints
  • lookup of beans in the Registry
  • additional supported in Spring XML (see below in examples)
  • using Blueprint PropertyPlaceholder with Camel Properties component


The syntax to use Camel's property placeholder is to use {{key}} for example {{file.uri}} where file.uri is the property key.
You can use property placeholders in parts of the endpoint URI's which for example you can use placeholders for parameters in the URIs.


Camel provides a pluggable mechanism which allows 3rd part to provide their own resolver to lookup properties. Camel provides a default implementation which is capable of loading properties from the file system, classpath or Registry. You can prefix the locations with either:

  • ref: Camel 2.4: to lookup in the Registry
  • file: to load the from file system
  • classpath: to load from classpath (this is also the default if no prefix is provided)
  • blueprint: Camel 2.7: to use a specific OSGi blueprint placeholder service

Defining location

The PropertiesResolver 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:


Using system and environment variables in locations

Available as of Camel 2.7

The location now supports using placeholders for JVM system properties and OS environments variables.

For example:


In the location above we defined a location using the file scheme using the JVM system property with key karaf.home.

To use an OS environment variable instead you would have to prefix with env:


Where APP_HOME is an OS environment.

You can have multiple placeholders in the same location, such as:


Configuring in Java DSL

You have to create and register the PropertiesComponent under the name properties such as:

PropertiesComponent pc = new PropertiesComponent();
context.addComponent("properties", pc);

Configuring in Spring XML

Spring XML offers two variations to configure. You can define a spring bean as a PropertiesComponent which resembles the way done in Java DSL. Or you can use the <propertyPlaceholder> tag.

<bean id="properties" class="">
    <property name="location" value="classpath:com/mycompany/"/>

Using the <propertyPlaceholder> tag makes the configuration a bit more fresh such as:

<camelContext ...>
   <propertyPlaceholder id="properties" location="com/mycompany/"/>

Specifying the cache option inside XML

Camel 2.10 onwards supports specifying a value for the cache option both inside the Spring as well as the Blueprint XML.

Using a Properties from the Registry

Available as of Camel 2.4
For example in OSGi you may want to expose a service which returns the properties as a java.util.Properties object.

Then you could setup the Properties component as follows:

   <propertyPlaceholder id="properties" location="ref:myProperties"/>

Where myProperties is the id to use for lookup in the OSGi registry. Notice we use the ref: prefix to tell Camel that it should lookup the properties for the Registry.

Examples using properties component

When using property placeholders in the endpoint URIs you can either use the properties: component or define the placeholders directly in the URI. We will show example of both cases, starting with the former.

// properties

// route

You can also use placeholders as a part of the endpoint uri:

// properties

// route

In the example above the to endpoint will be resolved to mock:result.

You can also have properties with refer to each other such as:

// properties

// route

Notice how cool.concat refer to another property.

The properties: component also offers you to override and provide a location in the given uri using the locations option:



You can also use property placeholders directly in the endpoint uris without having to use properties:.

// properties

// route

And you can use them in multiple wherever you want them:

// properties

// route

You can also your property placeholders when using ProducerTemplate for example:

template.sendBody("{{cool.start}}", "Hello World");

Example with Simple language

The Simple language now also support using property placeholders, for example in the route below:

// properties
cheese.quote=Camel rocks

// route
    .transform().simple("Hi ${body} do you think ${properties:cheese.quote}?");

You can also specify the location in the Simple language for example:

bar.quote=Beer tastes good

// route
    .transform().simple("Hi ${body}. ${properties:com/mycompany/}.");

Additional property placeholder supported in Spring XML

The property placeholders is also supported in many of the Camel Spring XML tags such as <package>, <packageScan>, <contextScan>, <jmxAgent>, <endpoint>, <routeBuilder>, <proxy> and the others.

The example below has property placeholder in the <jmxAgent> tag:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
<camelContext xmlns="">
    <propertyPlaceholder id="properties" location="org/apache/camel/spring/"/>

    <!-- we can use propery placeholders when we define the JMX agent -->
    <jmxAgent id="agent" registryPort="{{myjmx.port}}" disabled="{{myjmx.disabled}}"

    <route id="foo" autoStartup="false">
        <from uri="seda:start"/>
        <to uri="mock:result"/>


You can also define property placeholders in the various attributes on the <camelContext> tag such as trace as shown here:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
<camelContext trace="{{foo.trace}}" xmlns="">
    <propertyPlaceholder id="properties" location="org/apache/camel/spring/processor/"/>

    <template id="camelTemplate" defaultEndpoint="{{}}"/>

        <from uri="direct:start"/>
        <setHeader name="{{foo.header}}">
            <simple>${in.body} World!</simple>
        <to uri="mock:result"/>

Overriding a property setting using a JVM System Property

Available as of Camel 2.5
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

PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
System.setProperty("cool.end", "mock:override");
System.setProperty("cool.result", "override");

context.addRoutes(new RouteBuilder() {
    public void configure() throws Exception {


template.sendBody("direct:start", "Hello World");
template.sendBody("direct:foo", "Hello Foo");


Using property placeholders for any kind of attribute in the XML DSL

Available as of Camel 2.7

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.

In the example below we use the prop prefix for the namespace by which we can use the prop prefix in the attributes in the XML DSLs. Notice how we use that in the Multicast to indicate that the option stopOnException should be the value of the placeholder with the key "stop".

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
<beans xmlns=""

    <!-- Notice in the declaration above, we have defined the prop prefix as the Camel placeholder namespace -->

    <bean id="damn" class="java.lang.IllegalArgumentException">
        <constructor-arg index="0" value="Damn"/>

    <camelContext xmlns="">

        <propertyPlaceholder id="properties"

            <from uri="direct:start"/>
            <!-- use prop namespace, to define a property placeholder, which maps to
                 option stopOnException={{stop}} -->
            <multicast prop:stopOnException="stop">
                <to uri="mock:a"/>
                <throwException ref="damn"/>
                <to uri="mock:b"/>



In our properties file we have the value defined as


Using property placeholder in the Java DSL

Available as of Camel 2.7

Likewise we have added support for defining placeholders in the Java DSL using the new placeholder DSL as shown in the following equivalent example:

Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20

Using Blueprint property placeholder with Camel routes

Available as of Camel 2.7

Camel supports Blueprint 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:

Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20

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

About placeholder syntaxes

Notice how we can use the Camel syntax for placeholders {{ }} in the Camel route, which will lookup the value from OSGi blueprint.
The blueprint syntax for placeholders is ${ }. So outside the <camelContext> you must use the ${ } syntax. Where as inside <camelContext> you must use {{ }} syntax.
OSGi blueprint allows you to configure the syntax, so you can actually align those if you want.

You can also explicit refer to a specific OSGi blueprint property placeholder by its id. For that you need to use the Camel's <propertyPlaceholder> as shown in the example below:

Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20

Notice how we use the blueprint 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:


Each location is separated by comma.

Overriding Blueprint property placeholders outside CamelContext

Available as of Camel 2.10.4

When using Blueprint property placeholder in the Blueprint XML file, you can declare the properties directly in the XML file as shown below:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
<!-- blueprint property placeholders -->
<cm:property-placeholder persistent-id="my-placeholders" update-strategy="reload">
    <cm:property name="greeting" value="Hello"/>
    <cm:property name="destination" value="mock:result"/>

<!-- a bean that uses a blueprint property placeholder -->
<bean id="myCoolBean" class="org.apache.camel.test.blueprint.MyCoolBean">
  <property name="say" value="${greeting}"/>

<camelContext xmlns="">

    <from uri="direct:start"/>
    <bean ref="myCoolBean" method="saySomething"/>
    <to uri="{{destination}}"/>


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

Now if you want to override these Blueprint properties from an unit test, you can do this as shown below:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
protected String useOverridePropertiesWithConfigAdmin(Dictionary props) {
    // add the properties we want to override
    props.put("greeting", "Bye");

    // return the PID of the config-admin we are using in the blueprint xml file
    return "my-placeholders";

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

Using .cfg or .properties file for Blueprint property placeholders

Available as of Camel 2.10.4

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.

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.

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
<!-- blueprint property placeholders, that will use etc/stuff.cfg as the properties file -->
<cm:property-placeholder persistent-id="stuff" update-strategy="reload"/>

<!-- a bean that uses a blueprint property placeholder -->
<bean id="myCoolBean" class="org.apache.camel.test.blueprint.MyCoolBean">
  <property name="say" value="${greeting}"/>

<camelContext xmlns="">

    <from uri="direct:start"/>
    <bean ref="myCoolBean" method="saySomething"/>
    <to uri="mock:result"/>


Now if you want to unit test this blueprint XML file, then you can override the loadConfigAdminConfigurationFile and tell Camel which file to load as shown below:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
protected String[] loadConfigAdminConfigurationFile() {
    // String[0] = tell Camel the path of the .cfg file to use for OSGi ConfigAdmin in the blueprint XML file
    // String[1] = tell Camel the persistence-id of the cm:property-placeholder in the blueprint XML file
    return new String[]{"src/test/resources/etc/stuff.cfg", "stuff"};

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

The stuff.cfg file is just a plain properties file with the property placeholders such as:

## this is a comment

Bridging Spring and Camel property placeholders

Available as of Camel 2.10

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 org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer, which is a Spring org.springframework.beans.factory.config.PropertyPlaceholderConfigurer type.

To bridge Spring and Camel you must define a single bean as shown below:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'

<!-- 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:org/apache/camel/component/properties/"/>

You must not use the spring <context:property-placeholder> namespace at the same time; this is not possible.

After declaring this bean, you can define property placeholders using both the Spring style, and the Camel style within the <camelContext> tag as shown below:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'

<!-- a bean that uses Spring property placeholder -->
<!-- the ${hi} is a spring property placeholder -->
<bean id="hello" class="">
  <property name="greeting" value="${hi}"/>

<camelContext xmlns="">
  <!-- in this route we use Camels property placeholder {{ }} style -->
    <from uri="direct:{{}}"/>
    <bean ref="hello"/>
    <to uri="{{cool.end}}"/>

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 {{ }}.

Overriding properties from Camel test kit

Available as of Camel 2.10

When Testing with Camel and using the Properties component, you may want to be able to provide the properties to be used from directly within the unit test source code.
This is now possible from Camel 2.10 onwards, as the Camel test kits, eg CamelTestSupport class offers the following methods

  • useOverridePropertiesWithPropertiesComponent
  • ignoreMissingLocationWithPropertiesComponent

So for example in your unit test classes, you can override the useOverridePropertiesWithPropertiesComponent method and return a java.util.Properties that contains the properties which should be preferred to be used.

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
// override this method to provide our custom properties we use in this unit test
protected Properties useOverridePropertiesWithPropertiesComponent() {
    Properties extra = new Properties();
    extra.put("destination", "mock:extra");
    extra.put("greeting", "Bye");
    return extra;

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

The ignoreMissingLocationWithPropertiesComponent 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.

  • No labels