57.14. Lazy Injection / Programmatic Lookup
CDI プログラムによるモデルはアプリケーションの初期化時に発生する タイプセーフ解決 メカニズムを優先しますが、後でプログラムに よるルックアップ メカニズムを使用してアプリケーションの実行時に動的な/レイジーインジェクションを実行できます。
Camel CDI は、Camel プリミティブの標準インジェクションに使用できる CDI 修飾子に対応するアノテーションリテラルを提供します。これらのアノテーションリテラルは、レイジーインジェクション/プログラムによるルックアップを実行する CDI エントリーポイントである javax.enterprise.inject.Instance
インターフェースと組み合わせて使用できます。
たとえば、@Uri
修飾子に提供されたアノテーションリテラルを使用して、ProducerTemplate
Bean などの Camel プリミティブの遅延検索を行うことができます。
@Any @Inject Instance<ProducerTemplate> producers; ProducerTemplate inbound = producers .select(Uri.Literal.of("direct:inbound")) .get();
または、エンドポイント
Bean 用です。以下に例を示します。
@Any @Inject Instance<Endpoint> endpoints; MockEndpoint outbound = endpoints .select(MockEndpoint.class, Uri.Literal.of("mock:outbound")) .get();
同様に、@ContextName
修飾子に提供されたアノテーションリテラルを使用して、CamelContext
Bean の遅延検索を行うことができます。以下に例を示します。
@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);