The basic bean bindings described in Initializing parameters using method signatures might not always be convenient to use. For example, if you have a legacy Java class that performs some data manipulation, you might want to extract data from an inbound exchange and map it to the arguments of an existing method signature. For this kind of bean binding, Fuse Mediation Router provides the following kinds of Java annotation:
Table 5.1 shows the annotations from the org.apache.camel Java package that you can use to inject message data into the arguments of a bean method.
Table 5.1. Basic bean annotations
| Annotation | Meaning | Parameter |
|---|---|---|
@Attachments | Binds to a list of attachments. | |
@Body | Binds to an inbound message body. | |
@Header | Binds to an inbound message header. | String name of the header. |
@Headers | Binds to a java.util.Map of the inbound message headers. | |
@OutHeaders | Binds to a java.util.Map of the outbound message headers. | |
@Property | Binds to a named exchange property. | String name of the property. |
@Properties | Binds to a java.util.Map of the exchange properties. |
For example, Example 5.4 shows you how to use
basic annotations to inject message data into the processExchange()
method arguments.
Example 5.4. Using basic annotations to inject data into a bean
import org.apache.camel.*;
public class MyBeanProcessor {
public void processExchange(
@Header(name="user") String user,
@Body String body,
Exchange exchange
) {
// Do whatever you like to 'exchange'...
exchange.getIn().setBody(body + "UserName = " + user);
}
}Notice how you are able to mix the annotations with the default conventions. As well as
injecting the annotated arguments, the bean binding also automatically injects the exchange
object into the org.apache.camel.Exchange argument.
The expression language annotations provide a powerful mechanism for injecting message data into a bean method's arguments. Using these annotations, you can invoke an arbitrary script, written in the scripting language of your choice, to extract data from an inbound exchange and inject the data into a method argument.
Table 5.2 shows the annotations from the org.apache.camel.language package (and sub-packages, for the non-core annotations) that you can use to inject message data into the arguments of a bean method.
Table 5.2. Expression language annotations
| Annotation | Description |
|---|---|
@Bean | Injects a Bean expression. |
@BeanShell | Injects a BeanShell expression. |
@Constant | Injects a Constant expression |
@EL | Injects an EL expression. |
@Groovy | Injects a Groovy expression. |
@Header | Injects a Header expression. |
@JavaScript | Injects a JavaScript expression. |
@OGNL | Injects an OGNL expression. |
@PHP | Injects a PHP expression. |
@Python | Injects a Python expression. |
@Ruby | Injects a Ruby expression. |
@Simple | Injects a Simple expression. |
@XPath | Injects an XPath expression. |
@XQuery | Injects an XQuery expression. |
For example, Example 5.5 shows you how to use the
@XPath annotation to extract a username and a password from the body of an
incoming XML message.
Example 5.5. Using XPath to inject data into a bean
import org.apache.camel.language.*;
public class MyBeanProcessor {
public void checkCredentials(
@XPath("/credentials/username") String user,
@XPath("/credentials/password") String pass
) {
// Check the user/pass credentials...
...
}
}![]() | Note |
|---|---|
Some of the language annotations are available in the core component
( |
The @Bean annotation is a special case, because it enables you to inject
the result of invoking a registered bean. For example,
Example 5.6 shows a bean that uses the
@Bean annotation to invoke an ID generator class and inject the result into a
method argument.
Example 5.6. Using the @Bean annotation to inject data into a bean
import org.apache.camel.language.*;
public class MyBeanProcessor {
public void processCorrelatedMsg(
@Bean("myCorrIdGenerator") String corrId,
@Body String body
) {
// Check the user/pass credentials...
...
}
}The string, myCorrIdGenerator, is the bean ID of the ID generator
instance. The ID generator class can be instantiated using the Spring
bean element, as follows:
<beans ... > ... <bean id="myCorrIdGenerator" class="com.acme.MyIdGenerator" /> </beans>






![[Note]](imagesdb/note.gif)


