59.14. lazy Injection / Programmatic Lookup
虽然 CDI 程序化模型使用一种在应用程序初始化时发生 的类型安全解析 机制,但稍后可以使用程序化 查找 机制在应用程序执行期间执行动态 / lazy 注入。
Camel CDI 为方便使用 CDI 限定语对应的注解提供方便,供您用于标准注入 Camel 原语。这些注解文字可与 javax.enterprise.inject.Instance 接口结合使用,后者是执行 lazy 注入 / programmatic lookup 的 CDI 入口点。
例如,您可以使用 @Uri qualifier 提供的注解文字来对 Camel 原语(如 ProducerTemplate beans)进行 lazily 查询:
@Any
@Inject
Instance<ProducerTemplate> producers;
ProducerTemplate inbound = producers
.select(Uri.Literal.of("direct:inbound"))
.get();
或者对于 Endpoint Bean,例如:
@Any
@Inject
Instance<Endpoint> endpoints;
MockEndpoint outbound = endpoints
.select(MockEndpoint.class, Uri.Literal.of("mock:outbound"))
.get();
同样,您可以使用 @ContextName 限定符提供的注解文字来 lazily lookup for CamelContext beans,例如:
@Any
@Inject
Instance<CamelContext> contexts;
CamelContext context = contexts
.select(ContextName.Literal.of("foo"))
.get();您还可以根据 Camel 上下文类型优化选择,例如:
@Any
@Inject
Instance<CamelContext> contexts;
// Refine the selection by type
Instance<DefaultCamelContext> context = contexts.select(DefaultCamelContext.class);
// Check if such a bean exists then retrieve a reference
if (!context.isUnsatisfied())
context.get();或者迭代 Camel 上下文的选择,例如:
@Any
@Inject
Instance<CamelContext> contexts;
for (CamelContext context : contexts)
context.setUseBreadcrumb(true);