= Bean Method Language
:doctitle: Bean Method
:shortname: bean
:artifactid: camel-bean
:description: Calls a Java bean method.
:since: 1.3
:supportlevel: Stable
:tabs-sync-option:
//Manually maintained attributes
:camel-spring-boot-name: bean

*Since Camel {since}*

The Bean language is used for calling a method on an existing Java bean.

Camel adapts to the method being called via xref:manual::bean-binding.adoc[Bean Binding].
The binding process will, for example, automatically convert the message payload to the parameter
of type of the first parameter in the method.
The binding process has a lot more features, so it is recommended to read the xref:manual::bean-binding.adoc[Bean Binding] documentation for more details.

== Bean Method options

// language options: START
include::partial$language-options.adoc[]
// language options: END

== Examples

In the given route below, we call a Java Bean Method with `method`, where "myBean"
is the id of the bean to use (lookup from xref:manual::registry.adoc[Registry]),
and "isGoldCustomer" is the name of the method to call.

[tabs]
====
Java::
+
[source,java]
----
from("activemq:topic:OrdersTopic")
  .filter().method("myBean", "isGoldCustomer")
    .to("activemq:BigSpendersQueue");
----
+
[TIP]
=====
It is also possible to omit the method name. In this case, then Camel would choose the best suitable
method to use. This process is complex, so it is good practice to specify the method name.
=====

XML::
+
[source,xml]
----
<route>
  <from uri="activemq:topic:OrdersTopic"/>
  <filter>
    <method ref="myBean" method="isGoldCustomer"/>
    <to uri="activemq:BigSpendersQueue"/>
  </filter>
</route>
----
====

The bean could be implemented as follows:

[source,java]
----
public class MyBean {
  public boolean isGoldCustomer(Exchange exchange) {
     // ...
  }
}
----

How this method uses `Exchange` in the method signature. You would often not do that,
and use non-Camel types. For example, by using `String` then Camel will automatically
convert the message body to this type when calling the method:

[source,java]
----
public boolean isGoldCustomer(String body) {...}
----

=== Using Annotations for bean integration

You can also use the xref:manual::bean-integration.adoc[Bean Integration]
annotations, such as `@Header`, `@Body`, `@Variable` etc

[source,java]
----
public boolean isGoldCustomer(@Header(name = "foo") Integer fooHeader) {...}
----

So you can bind parameters of the method to the `Exchange`, the
xref:eips:message.adoc[Message] or individual headers, properties, the body
or other expressions.

=== Non-Registry Beans

The Bean Method Language also supports invoking beans
that are not registered in the xref:manual::registry.adoc[Registry].

Camel can instantiate the bean of a given type and invoke the method
or invoke the method on an already existing instance.

[source,java]
----
from("activemq:topic:OrdersTopic")
  .filter().method(MyBean.class, "isGoldCustomer")
  .to("activemq:BigSpendersQueue");
----

The first parameter can also be an existing instance of a Bean such as:

[source,java]
----
private MyBean my = ...;

from("activemq:topic:OrdersTopic")
  .filter().method(my, "isGoldCustomer")
  .to("activemq:BigSpendersQueue");
----


include::spring-boot:partial$starter.adoc[]
