Package org.apache.camel.support
Class PropertyBindingSupport
- java.lang.Object
-
- org.apache.camel.support.PropertyBindingSupport
-
public final class PropertyBindingSupport extends Object
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 with # or #bean: eg #myBean or #bean:myBean
- 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) .
- ignore case - Whether to ignore case for property keys
foo=123 ?bar=false
Where foo is mandatory, and bar is optional.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
PropertyBindingSupport.Builder
To use a fluent builder style to configure this property binding support.static interface
PropertyBindingSupport.OnAutowiring
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
autowireSingletonPropertiesFromRegistry(org.apache.camel.CamelContext camelContext, Object target)
This will discover all the properties on the target, and automatic bind the properties that are null by looking up in the registry to see if there is a single instance of the same type as the property.static boolean
autowireSingletonPropertiesFromRegistry(org.apache.camel.CamelContext camelContext, Object target, boolean bindNullOnly, boolean deepNesting, PropertyBindingSupport.OnAutowiring callback)
This will discover all the properties on the target, and automatic bind the properties by looking up in the registry to see if there is a single instance of the same type as the property.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.static PropertyBindingSupport.Builder
build()
static Object
resolveBean(org.apache.camel.CamelContext camelContext, String name, Object value)
Resolves the value as either a class, type or bean.
-
-
-
Method Detail
-
build
public static PropertyBindingSupport.Builder build()
-
autowireSingletonPropertiesFromRegistry
public static boolean autowireSingletonPropertiesFromRegistry(org.apache.camel.CamelContext camelContext, Object target)
This will discover all the properties on the target, and automatic bind the properties that are null by looking up in the registry to see if there is a single instance of the same type as the property. This is used for convention over configuration to automatic configure resources such as DataSource, Amazon Logins and so on.- Parameters:
camelContext
- the camel contexttarget
- the target object- Returns:
- true if one ore more properties was auto wired
-
autowireSingletonPropertiesFromRegistry
public static boolean autowireSingletonPropertiesFromRegistry(org.apache.camel.CamelContext camelContext, Object target, boolean bindNullOnly, boolean deepNesting, PropertyBindingSupport.OnAutowiring callback)
This will discover all the properties on the target, and automatic bind the properties by looking up in the registry to see if there is a single instance of the same type as the property. This is used for convention over configuration to automatic configure resources such as DataSource, Amazon Logins and so on.- Parameters:
camelContext
- the camel contexttarget
- the target objectbindNullOnly
- whether to only autowire if the property has no default value or has not been configured explicitdeepNesting
- whether to attempt to walk as deep down the object graph by creating new empty objects on the way if needed (Camel can only create new empty objects if they have a default no-arg constructor, also mind that this may lead to creating many empty objects, even if they will not have any objects autowired from the registry, so use this with caution)callback
- optional callback when a property was auto wired- Returns:
- true if one ore more properties was auto wired
-
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 where the bound properties will be removed from- Returns:
- true if one or more properties was bound
- See Also:
build()
-
resolveBean
public static Object resolveBean(org.apache.camel.CamelContext camelContext, String name, Object value) throws Exception
Resolves the value as either a class, type or bean.- Parameters:
camelContext
- the camel contextname
- the name of the beanvalue
- 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.
-
-