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

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 has a limitation that prevents third-party frameworks from fully leveraging Spring property placeholders.

For more details see: How do I use Spring Property Placeholder with Camel XML.

Bridging Spring and Camel Property Placeholders

From Camel 2.10: Spring's property placeholder can be bridged with Camel's. See below for more details.

The property placeholder is typically used when trying to do any of the following:

  • 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.
  • Using @PropertyInject to inject a property in a POJO.
  • Camel 2.14.1 Using default value if a property does not exists.
  • Camel 2.14.1 Include out of the box functions, to lookup property values from OS environment variables, JVM system properties, or the service idiom.
  • Camel 2.14.1 Using custom functions, which can be plugged into the property component.

Format

The value of a Camel property can be obtained by specifying its key name within a property placeholder, using the following format: {{key}}.

For example:

where file.uri is the property key.

Property placeholders can be used to specify parts, or all, of an endpoint's URI by embedding one or more placeholders in the URI's string definition.

From Camel 2.14.1: you can specify a default value to use if a property with the key does not exists, e.g., file.url:/some/path where the default value is the text after the colon, e.g., /some/path.

From Camel 2.14.1: do not use a colon in the property key. The colon character is used as a token separator when providing a default value.

Using PropertyResolver

Camel provides a pluggable mechanism that allows third-parties to specify their own resolver to use for the lookup of properties.

Camel provides a default implementation org.apache.camel.component.properties.DefaultPropertiesResolver which is capable of loading properties from the file system, classpath or Registry. To indicate which source to use the location must contain the appropriate prefix.

The list of prefixes is:

Prefix

Description

ref:

Lookup in the Registry.

file:

Load the from file system.

classpath:

Load from the classpath (this is also the default if no prefix is provided).

blueprint:

Use a specific OSGi blueprint placeholder service.

Defining Location

The PropertiesResolver must be configured with the location(s) to use when resolving properties. One or more locations can be given. Specifying multiple locations can be done a couple of ways: using either a single comma separated string, or an array of strings.

java

From Camel 2.19.0: you can set which location can be discarded if missing by setting  optional=true, (false by default).

Example:

 

java

 

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.

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 variable.

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

Using System or Environment Variables to Configure Property Prefixes and Suffixes

From Camel 2.12.5, 2.13.3, 2.14.0: propertyPrefix, propertySuffix configuration properties support the use of placeholders for de-referencing JVM system properties and OS environments variables.

Example:

Assume the PropertiesComponent is configured with the following properties file:

text

The same properties file is then referenced from a route definition:

java

By using the configuration options propertyPrefix it's possible to change the target endpoint simply by changing the value of the system property stage either to dev (the message will be routed to mock:result1) or test (the message will be routed to mock:result2).

Configuring in Java DSL

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

java

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.

xml ]]>

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

xml ]]>

Setting the properties location through the location tag works just fine but sometime you have a number of resources to take into account and starting from Camel 2.19.0 you can set the properties location with a dedicated propertiesLocation:

xml ]]>Specifying the cache option in XML

From Camel 2.10: Camel 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:

xml ]]>

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.

java

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

java

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:

java

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:

java

Examples

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

java

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

java

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

java

Example with Simple language

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

java

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

java

Additional Property Placeholder Support 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.

Example:

xmlUsing property placeholders in the <jmxAgent> tag ]]>

Example:

xmlUsing property placeholders in the attributes of <camelContext>