concept @inject in category camel

appears as: @Inject
Camel in Action

This is an excerpt from Manning's book Camel in Action.

But we’ve chosen to show a practice often used with CDI, which is dependency injection, and why we inject the endpoints also. To call the bean, you can use bean("helloBean", "hello") ❹ to refer to the bean by its name and the name of the method to invoke. But you also could have injected the bean by using CDI. This can be done using @Inject and @Named, as shown in the following code snippet:

Let’s raise the bar one more time and show you the most-used feature of CDI: using CDI dependency injection in your POJO beans with @Inject.

Listing 7.3 Injecting bean using CDI @Inject
@Singleton public class HelloRoute extends RouteBuilder { @Inject     #1 private HelloBean hello; public void configure() throws Exception { from("jetty:http://localhost:8080/hello") .bean(hello, "sayHello");     #2 } } #1 Dependency injects the HelloBean using @Inject #2 Calls the method sayHello on the injected HelloBean instance

Because you want to use the HelloBean in the Camel route, you can instruct CDI to dependency-inject an instance of the bean by declaring a field and annotate the field with @Inject ❶. The bean can then be used in the Camel route as a bean method call ❷.

This example is provided with the book’s source code in the chapter7/cdi directory. With the source code, you see how to use @Inject in unit tests with CDI. We’ve devoted all of chapter 9 to the topic of testing and cover testing CDI in much more detail.