Bean Integration (Route Definition)
Bean integration provides a general-purpose method for processing messages using arbitrary POJO objects within our routes. Through the insertion of a bean reference into a route, we can call a method on a POJO Java object, which will then have the ability to access and modify the incoming exchange. The mechanism that maps an exchange’s contents to the parameters and return values of a bean method is known as parameter binding. Parameter binding can use any combination of the following approaches in order to initialize a method’s parameters:
Conventional method signatures  —  If the method signature conforms to certain conventions, the parameter binding can use Java reflection to determine what parameters to pass.
Annotations and dependency injection  —  For a more flexible binding mechanism, we can employ Java annotations to specify what to inject into the method’s arguments. This dependency injection mechanism relies on Spring 2.5 component scanning. If you are deploying your Apache Camel application into a Spring container, the dependency injection mechanism will work automatically.
Explicitly specified parameters  —  You can specify parameters explicitly (either as constants or using the Simple language), at the point where the bean is invoked.
Bean registry
Beans are made accessible and visible to us through a bean registry, this is a service that enables you to lookup beans via the class name or the bean ID as a key. The way that you create an entry in the bean registry depends on the underlying framework (ie. Java, Spring, etc). All Registry entries are created implicitly (ie. instantiate a Spring bean in a Spring XML file).
Accessing a bean created in Java
To process exchange objects using a Java bean (which is a plain old Java object or POJO), use the bean()
processor, which binds the inbound exchange to a method on the Java object. For example, to process inbound exchanges using the class, MyBeanProcessor
, define a route like the following:
from("file:data/inbound")
.bean(MyBeanProcessor.class, "processBody")
.to("file:data/outbound");
Â
<route>
<from uri="file:data/inbound"/>
<bean ref="MyBeanProcessor" method="processBody"/>
<to uri="file:data/outbound"/>
</route>
Method Signature to Process Message
If we need to implement a method on a bean that will access or update an incoming message body, then we must define a method signature that takes a single String
argument and returns a String
value. For example:
package com.acme;
public class MyBeanProcessor {
public String processBody(String body) {
// Do whatever you like to 'body'...
return newBody;
}
}
Method Signature tp Process Exchange
For even greater flexibility, we can implement a bean method that will access the incoming exchange. This will enable us to access or modify all headers, bodies, and exchange properties.
Spring Bean from Spring XML
Rather than creating a bean instance in Java, we can create a bean instance using Spring XML. In fact, this is the only feasible approach if you are defining your routes in XML. To define a bean in XML, use the standard Spring bean
element.
Where the processor invokes the MyBeanProcessor.processBody()
method on the bean instance. We can also invoke the bean from within a Spring XML route, using the Camel schema’s bean
element.
Spring Bean using Injection
Alternatively, you can reference the Spring bean by injection, using the @BeanInject
annotation:
NOTE: If we omit the bean ID from the @BeanInject
annotation, then it will perform the lookup within the bean registry based on the type. However, this only works if there is just a single bean of the given type.
Basic Annotations
Annotation | Meaning | Parameters |
---|---|---|
| Binds to a list of attachments | Â |
| Binds to an inbound message body. | Â |
| Binds to an inbound message header. | String name of the header. |
| Binds to a | Â |
| Binds to a | Â |
| Binds to a named exchange property. | String name of the property. |
| Binds to a | Â |
For example, the following example will show the use of basic annotations to inject message data into the processExchange()
method arguments.
Expression Language Annotations
Another option is the use of the expression language annotations, they provide a powerful and flexible method for injecting message data into a bean method’s arguments. With these annotations, we can invoke an arbitrary script, written in your chosen scripting language, to extract data from an inbound exchange and then inject the data into a method argument.
Annotation | Description |
---|---|
| Injects a Bean expression. |
| Injects a Constant expression |
| Injects an EL expression. |
| Injects a Groovy expression. |
| Injects a Header expression. |
| Injects a JavaScript expression. |
| Injects an OGNL expression. |
| Injects a PHP expression. |
| Injects a Python expression. |
| Injects a Ruby expression. |
| Injects a Simple expression. |
| Injects an XPath expression. |
| Injects an XQuery expression. |
For example, the following example shows how to use @XPath
annotation to extract a username and a password from the body of an incoming message in XML format:
Â
Invoke Static Methods
The Bean integration process also has the capacity to invoke static methods without creating an instance of the associated class. For example, consider the following that defines the static method, changeSomething()
:
Use bean integration to invoke the static changeSomething
method, as follows: