[[bean-eip]]
= Bean EIP
:docTitle: Bean
:description: Calls a java bean
:since: 
:supportLevel: Stable

The Bean EIP binds beans to Camel message exchanges.

== URI Format

[source]
----
bean:beanID[?options]
----

Where *beanID* can be any string which is used to look up the bean in
the xref:manual:ROOT:registry.adoc[Registry]

== EIP options

// eip options: START
The Bean EIP supports 5 options which are listed below:

[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *ref* | Sets a reference to a bean to use |  | String
| *method* | Sets the method name on the bean to use |  | String
| *beanType* | Sets the Class of the bean |  | String
| *cache* | *Deprecated* Use singleton option instead | true | Boolean
| *scope* | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while processing the request. The bean does not have to be thread-safe as the instance is only called from the same request. When using prototype scope, then the bean will be looked up or created per call. However in case of lookup then this is delegated to the bean registry such as Spring or CDI (if in use), which depends on their configuration can act as either singleton or prototype scope. so when using prototype scope then this depends on the bean registry implementation. | Singleton | String
|===
// eip options: END

=== Bean scope

When using `singleton` scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint.
The bean should be thread-safe in case concurrent threads is calling the bean at the same time.
When using `request` scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean
while processing a request and you want to call the same bean instance multiple times while processing the request.
The bean does not have to be thread-safe as the instance is only called from the same request.
When using `prototype` scope, then the bean will be looked up or created per call. However in case of lookup then this is delegated
to the bean registry such as Spring or CDI (if in use), which depends on their configuration can act as either singleton or prototype scope.
so when using `prototype` then this depends on the delegated registry.

== Bean as endpoint

Camel also supports invoking xref:ROOT:bean-component.adoc[Bean] as an Endpoint. In the
route below:

What happens is that when the exchange is routed to the `myBean` Camel
will use the xref:manual:ROOT:bean-binding.adoc[Bean Binding] to invoke the bean. +
 The source for the bean is just a plain POJO:

Camel will use xref:manual:ROOT:bean-binding.adoc[Bean Binding] to invoke the
`sayHello` method, by converting the Exchange's In body to the `String`
type and storing the output of the method on the Exchange Out body.

== Java DSL bean syntax

Java DSL comes with syntactic sugar for the xref:ROOT:bean-component.adoc[Bean]
component. Instead of specifying the bean explicitly as the endpoint
(i.e. `to("bean:beanName")`) you can use the following syntax:

[source,java]
----
// Send message to the bean endpoint
// and invoke method resolved using Bean Binding.
from("direct:start").beanRef("beanName");

// Send message to the bean endpoint
// and invoke given method.
from("direct:start").beanRef("beanName", "methodName");
----

Instead of passing name of the reference to the bean (so that Camel will
lookup for it in the registry), you can specify the bean itself:

[source,java]
----
// Send message to the given bean instance.
from("direct:start").bean(new ExampleBean());

// Explicit selection of bean method to be invoked.
from("direct:start").bean(new ExampleBean(), "methodName");

// Camel will create the instance of bean and cache it for you.
from("direct:start").bean(ExampleBean.class);
----

== Bean binding

How bean methods to be invoked are chosen (if they are not specified
explicitly through the *method* parameter) and how parameter values are
constructed from the xref:message.adoc[Message] are all defined by the
xref:manual:ROOT:bean-binding.adoc[Bean Binding] mechanism which is used throughout
all of the various xref:manual:ROOT:bean-integration.adoc[Bean Integration]
mechanisms in Camel.
