Chapter 5. Contexts and Dependency Injection (CDI) in Camel Quarkus
CDI plays a central role in Quarkus and Camel Quarkus offers a first class support for it too.
You may use @Inject
, @ConfigProperty
and similar annotations e.g. to inject beans and configuration values to your Camel RouteBuilder
, for example:
- 1
- The
@ApplicationScoped
annotation is required for@Inject
and@ConfigProperty
to work in aRouteBuilder
. Note that the@ApplicationScoped
beans are managed by the CDI container and their life cycle is thus a bit more complex than the one of the plainRouteBuilder
. In other words, using@ApplicationScoped
inRouteBuilder
comes with some boot time penalty and you should therefore only annotate yourRouteBuilder
with@ApplicationScoped
when you really need it. - 2
- The value for the
timer.period
property is defined insrc/main/resources/application.properties
of the example project.
Please refer to the Quarkus Dependency Injection guide for more details.
5.1. Accessing CamelContext リンクのコピーリンクがクリップボードにコピーされました!
To access CamelContext
just inject it into your bean:
5.2. CDI and the Camel Bean component リンクのコピーリンクがクリップボードにコピーされました!
5.2.1. Refer to a bean by name リンクのコピーリンクがクリップボードにコピーされました!
To refer to a bean in a route definition by name, just annotate the bean with @Named("myNamedBean")
and @ApplicationScoped
. The @RegisterForReflection
annotation is important for the native mode.
Then you can use the myNamedBean
name in a route definition: