Package org.apache.camel.support
Class PropertyBindingSupport
java.lang.Object
org.apache.camel.support.PropertyBindingSupport
A convenient support class for binding String valued properties to an instance which uses a set of conventions:
- property placeholders - Keys and values using Camels property placeholder will be resolved
- nested - Properties can be nested using the dot syntax (OGNL and builder pattern using with as prefix), eg foo.bar=123
- map - Properties can lookup in Map's using map syntax, eg foo[bar] where foo is the name of the property that is a Map instance, and bar is the name of the key.
- list - Properties can refer or add to in List's using list syntax, eg foo[0] where foo is the name of the property that is a List instance, and 0 is the index. To refer to the last element, then use last as key.
- reference by property placeholder id - Values can refer to a property placeholder key with #property:myKey
- reference by bean id - Values can refer to other beans in the registry by prefixing with # or #bean: eg #myBean or #bean:myBean. It is recommended to favour using `#bean:` syntax to make it obvious it's a bean reference.
- reference by type - Values can refer to singleton beans by their type in the registry by prefixing with #type: syntax, eg #type:com.foo.MyClassType
- autowire by type - Values can refer to singleton beans by auto wiring by setting the value to #autowired
- reference new class - Values can refer to creating new beans by their class name by prefixing with #class, eg #class:com.foo.MyClassType. The class is created using a default no-arg constructor, however if you need to create the instance via a factory method then you specify the method as shown: #class:com.foo.MyClassType#myFactoryMethod. And if the factory method requires parameters they can be specified as follows: #class:com.foo.MyClassType#myFactoryMethod('Hello World', 5, true). Or if you need to create the instance via constructor parameters then you can specify the parameters as shown: #class:com.foo.MyClass('Hello World', 5, true) .
- valueAs(type):value - To declare that the value should be converted to the given type, such as #valueAs(int):123 which indicates that the value 123 should be converted to an integer.
- ignore case - Whether to ignore case for property keys
Keys with dash style is supported and will internally be converted from dash to camel case style (eg queue-connection-factory => queueConnectionFactory)
Keys can be marked as optional if the key name starts with a question mark, such as:
foo=123 ?bar=false
Where foo is mandatory, and bar is optional.
Values can be marked as optional property placeholder if the values name starts with a question mark, such as:
username={{?clientUserName}}
Where the username property will only be set if the property placeholder clientUserName exists, otherwise the username is not affected.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
To use a fluent builder style to configure this property binding support.static interface
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
bindProperties
(org.apache.camel.CamelContext camelContext, Object target, Map<String, Object> properties) Binds the properties to the target object, and removes the property that was bound from properties.static boolean
bindWithFlattenProperties
(org.apache.camel.CamelContext camelContext, Object target, Map<String, Object> properties) Binds the properties to the target object, and removes the property that was bound from properties.build()
static Object
newInstanceConstructorParameters
(org.apache.camel.CamelContext camelContext, Class<?> type, String parameters) Creates a new bean instance using the constructor that takes the given set of parameters.static Object
newInstanceFactoryParameters
(org.apache.camel.CamelContext camelContext, Class<?> type, String factoryMethod, String parameters) Creates a new bean instance using a public static factory method from the given classstatic Object
resolveBean
(org.apache.camel.CamelContext camelContext, Object value) Resolves the value as either a class, type or bean.static void
setPropertiesOnTarget
(org.apache.camel.CamelContext context, Object target, Map<String, Object> properties) Sets the properties to the given target.
-
Method Details
-
build
-
bindProperties
public static boolean bindProperties(org.apache.camel.CamelContext camelContext, Object target, Map<String, Object> properties) Binds the properties to the target object, and removes the property that was bound from properties. This method uses the default settings, and if you need to configure any setting then use the fluent builderbuild()
where each option can be customized, such as whether parameter should be removed, or whether options are mandatory etc.- Parameters:
camelContext
- the camel contexttarget
- the target objectproperties
- the properties (as flat key=value paris) where the bound properties will be removed- Returns:
- true if one or more properties was bound
- See Also:
-
bindWithFlattenProperties
public static boolean bindWithFlattenProperties(org.apache.camel.CamelContext camelContext, Object target, Map<String, Object> properties) Binds the properties to the target object, and removes the property that was bound from properties. This method uses the default settings, and if you need to configure any setting then use the fluent builderbuild()
where each option can be customized, such as whether parameter should be removed, or whether options are mandatory etc.- Parameters:
camelContext
- the camel contexttarget
- the target objectproperties
- the properties as (map of maps) where the properties will be flattened, and bound properties will be removed- Returns:
- true if one or more properties was bound
- See Also:
-
setPropertiesOnTarget
public static void setPropertiesOnTarget(org.apache.camel.CamelContext context, Object target, Map<String, Object> properties) Sets the properties to the given target.- Parameters:
context
- the context into which the properties must be set.target
- the object to which the properties must be set.properties
- the properties to set.
-
newInstanceConstructorParameters
public static Object newInstanceConstructorParameters(org.apache.camel.CamelContext camelContext, Class<?> type, String parameters) throws Exception Creates a new bean instance using the constructor that takes the given set of parameters.- Parameters:
camelContext
- the camel contexttype
- the class type of the bean to createparameters
- the parameters for the constructor- Returns:
- the created bean, or null if there was no constructor that matched the given set of parameters
- Throws:
Exception
- is thrown if error creating the bean
-
newInstanceFactoryParameters
public static Object newInstanceFactoryParameters(org.apache.camel.CamelContext camelContext, Class<?> type, String factoryMethod, String parameters) throws Exception Creates a new bean instance using a public static factory method from the given class- Parameters:
camelContext
- the camel contexttype
- the class with the public static factory methodparameters
- optional parameters for the factory method- Returns:
- the created bean, or null if there was no factory method (optionally matched the given set of parameters)
- Throws:
Exception
- is thrown if error creating the bean
-
resolveBean
public static Object resolveBean(org.apache.camel.CamelContext camelContext, Object value) throws Exception Resolves the value as either a class, type or bean.- Parameters:
camelContext
- the camel contextvalue
- how to resolve the bean with a prefix of either #class:, #type: or #bean:- Returns:
- the resolve bean
- Throws:
Exception
- is thrown if error resolving the bean, or if the value is invalid.
-