Class CdiHelper

java.lang.Object
com.oracle.coherence.rag.util.CdiHelper

public class CdiHelper extends Object
Utility class for programmatic CDI bean lookup and retrieval.

This helper class provides static methods for retrieving CDI managed beans programmatically, which is useful in scenarios where dependency injection is not available or when dynamic bean lookup is required based on runtime configuration.

The class supports two primary lookup strategies:

  • Type-based lookup: Retrieving beans by their class type
  • Name-based lookup: Retrieving named beans with type filtering

All methods handle CDI bean resolution, ambiguity checking, and proper creation context management to ensure beans are properly initialized and managed by the CDI container.

Usage examples:

// Get bean by type
EmbeddingModel embeddingModel = CdiHelper.getBean(EmbeddingModel.class);

// Get named bean with type filtering
ModelProvider provider = CdiHelper.getNamedBean(ModelProvider.class, "OpenAI");

Since:
25.09
Author:
Aleks Seovic 2025.07.04
  • Constructor Details

    • CdiHelper

      public CdiHelper()
  • Method Details

    • getBean

      public static <T> T getBean(Class<T> type)
      Retrieves a CDI managed bean by its class type.

      This method performs type-based bean lookup using the CDI BeanManager. If multiple beans of the same type exist, CDI's resolution mechanism is used to select the appropriate bean (based on qualifiers, priorities, etc.).

      The method handles proper creation context management to ensure the returned bean is fully initialized and managed by the CDI container.

      Type Parameters:
      T - the type of the bean to retrieve
      Parameters:
      type - the Class object representing the bean type
      Returns:
      the bean instance of the specified type, or null if no bean found
    • getNamedBean

      public static <T> T getNamedBean(Class<T> type, String name)
      Retrieves a named CDI managed bean with type filtering.

      This method performs name-based bean lookup and then filters the results to ensure the bean is assignable to the specified type. This is particularly useful for retrieving named implementations of interfaces (such as model providers identified by name).

      The method includes ambiguity checking to ensure only one bean matches both the name and type criteria, throwing an exception if multiple matching beans are found.

      Type Parameters:
      T - the type of the bean to retrieve
      Parameters:
      type - the Class object representing the expected bean type
      name - the name qualifier of the bean to retrieve
      Returns:
      the named bean instance of the specified type, or null if no matching bean found
      Throws:
      IllegalStateException - if multiple beans match the name and type criteria