= Bean Method Language
:doctitle: Bean Method
:shortname: bean
:artifactid: camel-bean
:description: Calls a Java bean method.
:since: 1.3
:supportlevel: Stable
//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 automatic 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 mor 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.

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

TIP: It is also possible to omit the method name, then Camel would have to choose the best suitable
method to use; this process is a little bit complex, so it is good practice to specify the method name,

And in XML DSL

[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 automatic
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` 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 isn't registered in the xref:manual::registry.adoc[Registry].

Camel can instantiate the bean and invoke the method if given a class
or invoke an already existing instance.

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

The 1st 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[]
