= Bean Component
:doctitle: Bean
:shortname: bean
:artifactid: camel-bean
:description: Invoke methods of Java beans stored in Camel registry.
:since: 1.0
:supportlevel: Stable
:tabs-sync-option:
:component-header: Only producer is supported
:core:
//Manually maintained attributes
:camel-spring-boot-name: bean

*Since Camel {since}*

*{component-header}*

The Bean component binds beans to Camel message exchanges.

== URI format

----
bean:beanName[?options]
----

Where `beanName` can be any string used to look up the bean in the Registry

// component-configure options: START

// component-configure options: END

// component options: START
include::partial$component-configure-options.adoc[]
include::partial$component-endpoint-options.adoc[]
// component options: END

// endpoint options: START

// endpoint options: END

// component headers: START
include::partial$component-endpoint-headers.adoc[]
// component headers: END

== Examples

A *bean:* endpoint cannot be defined as the input to the route; i.e., you
cannot consume from it, you can only route from some inbound message
Endpoint to the bean endpoint as output, such as the *direct* endpoint as input.

Suppose you have the following POJO class to be used by Camel

[source,java]
----
package com.foo;

public class MyBean {

    public String saySomething(String input) {
        return "Hello " + input;
    }
}
----

Then the bean can be called in a Camel route by the fully qualified class name:

[tabs]
====
Java::
+
[source,java]
----
from("direct:hello")
   .to("bean:com.foo.MyBean");
----

XML::
+
[source,xml]
----------------------------
<route>
   <from uri="direct:hello"/>
   <to uri="bean:com.foo.MyBean"/>
</route>
----------------------------
====

What happens is that when the exchange is routed to the MyBean, then Camel
will use the Bean Binding to invoke the bean, in this case the _saySomething_ method,
by converting the `Exchange` in body to the `String`
type and storing the output of the method back to the Exchange again.

[TIP]
====
The bean component can also call a bean by _bean id_ by looking up the bean
in the xref:manual::registry.adoc[Registry] instead of using the class name.
====

=== Java DSL specific bean syntax

Java DSL comes with syntactic sugar for the xref: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 a message to the bean endpoint
// and invoke method using Bean Binding.
from("direct:start").bean("beanName");

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

Instead of passing the name of the reference to the bean (so that Camel will
look up for it in the xref:manual::registry.adoc[Registry]), you can specify the bean itself:

[source,java]
---------------------------------------------------------------
// Send a 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);
---------------------------------------------------------------

This bean could be a lambda if you cast the lambda to a `@FunctionalInterface`

[source,java]
---------------------------------------------------------------
@FunctionalInterface
public interface ExampleInterface() {
    @Handler String methodName();
}

from("direct:start")
    .bean((ExampleInterface) () -> ""))
---------------------------------------------------------------

== Bean Binding

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

See also related xref:languages:bean-language.adoc[Bean Language].

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