| = Class Component |
| :doctitle: Class |
| :shortname: class |
| :artifactid: camel-bean |
| :description: Invoke methods of Java beans specified by class name. |
| :since: 2.4 |
| :supportlevel: Stable |
| :tabs-sync-option: |
| :component-header: Only producer is supported |
| :core: |
| |
| *Since Camel {since}* |
| |
| *{component-header}* |
| |
| The Class component binds beans to Camel message exchanges. It works |
| in the same way as the xref:bean-component.adoc[Bean] component, but instead of |
| looking up beans from a Registry, it creates the bean |
| based on the class name. |
| |
| == URI format |
| |
| ---- |
| class:className[?options] |
| ---- |
| |
| Where `className` is the fully qualified class name to create and use as bean. |
| |
| |
| // component options: START |
| include::partial$component-configure-options.adoc[] |
| include::partial$component-endpoint-options.adoc[] |
| include::partial$component-endpoint-headers.adoc[] |
| // component options: END |
| |
| == Usage |
| |
| You simply use the *class* component just as the xref:bean-component.adoc[Bean] |
| component but by specifying the fully qualified class name instead. |
| For example to use the `MyFooBean` you have to do as follows: |
| |
| [tabs] |
| ==== |
| Java:: |
| + |
| [source,java] |
| ---- |
| from("direct:start") |
| .to("class:org.apache.camel.component.bean.MyFooBean") |
| .to("mock:result"); |
| ---- |
| |
| XML:: |
| + |
| [source,xml] |
| ---- |
| <route> |
| <from uri="direct:start"/> |
| <to uri="class:org.apache.camel.component.bean.MyFooBean"/> |
| <to uri="mock:result"/> |
| </route> |
| ---- |
| |
| YAML:: |
| + |
| [source,yaml] |
| ---- |
| - route: |
| from: |
| uri: direct:start |
| steps: |
| - to: |
| uri: class:org.apache.camel.component.bean.MyFooBean |
| - to: |
| uri: mock:result |
| ---- |
| ==== |
| |
| You can also specify which method to invoke on the `MyFooBean`, for |
| example `hello`: |
| |
| [tabs] |
| ==== |
| Java:: |
| + |
| [source,java] |
| ---- |
| from("direct:start") |
| .to("class:org.apache.camel.component.bean.MyFooBean?method=hello") |
| .to("mock:result"); |
| ---- |
| |
| XML:: |
| + |
| [source,xml] |
| ---- |
| <route> |
| <from uri="direct:start"/> |
| <to uri="class:org.apache.camel.component.bean.MyFooBean?method=hello"/> |
| <to uri="mock:result"/> |
| </route> |
| ---- |
| |
| YAML:: |
| + |
| [source,yaml] |
| ---- |
| - route: |
| from: |
| uri: direct:start |
| steps: |
| - to: |
| uri: class:org.apache.camel.component.bean.MyFooBean |
| parameters: |
| method: hello |
| - to: |
| uri: mock:result |
| ---- |
| ==== |
| |
| == Examples |
| |
| === Setting properties on the created instance |
| |
| In the endpoint uri you can specify properties to set on the created |
| instance, for example, if it has a `setPrefix` method: |
| |
| [tabs] |
| ==== |
| Java:: |
| + |
| [source,java] |
| ---- |
| from("direct:start") |
| .to("class:org.apache.camel.component.bean.MyPrefixBean?bean.prefix=Bye") |
| .to("mock:result"); |
| ---- |
| |
| XML:: |
| + |
| [source,xml] |
| ---- |
| <route> |
| <from uri="direct:start"/> |
| <to uri="class:org.apache.camel.component.bean.MyPrefixBean?bean.prefix=Bye"/> |
| <to uri="mock:result"/> |
| </route> |
| ---- |
| |
| YAML:: |
| + |
| [source,yaml] |
| ---- |
| - route: |
| from: |
| uri: direct:start |
| steps: |
| - to: |
| uri: class:org.apache.camel.component.bean.MyPrefixBean |
| parameters: |
| bean.prefix: Bye |
| - to: |
| uri: mock:result |
| ---- |
| ==== |
| |
| And you can also use the `#` syntax to refer to properties to be looked |
| up in the Registry. |
| |
| [tabs] |
| ==== |
| Java:: |
| + |
| [source,java] |
| ---- |
| from("direct:start") |
| .to("class:org.apache.camel.component.bean.MyPrefixBean?bean.cool=#foo") |
| .to("mock:result"); |
| ---- |
| |
| XML:: |
| + |
| [source,xml] |
| ---- |
| <route> |
| <from uri="direct:start"/> |
| <to uri="class:org.apache.camel.component.bean.MyPrefixBean?bean.cool=#foo"/> |
| <to uri="mock:result"/> |
| </route> |
| ---- |
| |
| YAML:: |
| + |
| [source,yaml] |
| ---- |
| - route: |
| from: |
| uri: direct:start |
| steps: |
| - to: |
| uri: class:org.apache.camel.component.bean.MyPrefixBean |
| parameters: |
| bean.cool: "#foo" |
| - to: |
| uri: mock:result |
| ---- |
| ==== |
| |
| Which will look up a bean from the Registry with the |
| id `foo` and invoke the `setCool` method on the created instance of the |
| `MyPrefixBean` class. |
| |
| [TIP] |
| ==== |
| See more details on the xref:bean-component.adoc[Bean] component as the *class* |
| component works in much the same way. |
| ==== |