Skip to content

Commit

Permalink
Document and test referring to beans by name
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga authored and lburgazzoli committed Nov 29, 2019
1 parent 9f0689f commit 074b969
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/modules/ROOT/pages/cdi.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,43 @@ you really need it.
<2> The value for the `timer.period` property is defined in `src/main/resources/application.properties` of the example project.

TIP: Please refer to the https://quarkus.io/blog/quarkus-dependency-injection[Quarkus Dependency Injection guide] for more details.


== CDI and the Camel Bean component

=== Refer to a bean by name

To refer to a bean in a route definition by name, just annotate the the bean with `@Named("myNamedBean")` and
`@ApplicationScoped` (or some other
https://quarkus.io/guides/cdi-reference#supported_features[supported] scope). The `@RegisterForReflection` annotation
is important for the native mode.

[source,java]
----
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import io.quarkus.runtime.annotations.RegisterForReflection;
@ApplicationScoped
@Named("myNamedBean")
@RegisterForReflection
public class NamedBean {
public String hello(String name) {
return "Hello " + name + " from the NamedBean";
}
}
----

Then you can use the `myNamedBean` name in a route definition:

[source,java]
----
import org.apache.camel.builder.RouteBuilder;
public class CamelRoute extends RouteBuilder {
@Override
public void configure() {
from("direct:named")
.to("bean:namedBean?method=hello");
}
}
----
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public String processOrder(String statement) {
return template.requestBody("direct:process-order", statement, String.class);
}

@Path("/named")
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String named(String statement) {
return template.requestBody("direct:named", statement, String.class);
}

@Path("/increment")
@GET
@Produces(MediaType.TEXT_PLAIN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public void configure() {
// log out
.to("log:out");

from("direct:named")
.to("bean:namedBean?method=hello")
.to("log:named");

}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.component.bean;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

import io.quarkus.runtime.annotations.RegisterForReflection;

/**
* A bean referenced from a route (and from nowhere else) by name.
*/
@ApplicationScoped
@Named("namedBean")
@RegisterForReflection // Let Quarkus register this class for reflection during the native build
public class NamedBean {

public String hello(String name) {
return "Hello " + name + " from the NamedBean";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public void testRoutes() {
.body(equalTo("1"));
}

@Test
public void named() {
RestAssured.given()
.contentType(ContentType.TEXT)
.body("Kermit")
.post("/bean/named")
.then()
.body(equalTo("Hello Kermit from the NamedBean"));
}

@Test
public void inject() {

Expand Down

0 comments on commit 074b969

Please sign in to comment.