Interface BeanProvider<T>

Type Parameters:
T - The generic bean type
All Superinterfaces:
Iterable<T>
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface BeanProvider<T> extends Iterable<T>
A BeanProvider is a richer replacement for the Provider interface that provides additional Micronaut specific methods to assist in programmatic bean creation and discovery.
Since:
2.4.0
Author:
James Kleeh, graemerocher
  • Method Details

    • get

      @NonNull T get()
      The get method will materialize an instance of the bean if it is resolvable.

      A bean is considered resolvable if it is both unique and present. See isUnique() and isPresent().

      Note that if the bean is Singleton then multiple calls to this method will return the same instance.

      Returns:
      A fully-constructed and injected instance of BeanProvider.
      Throws:
      BeanCreationException - If an error occurs during the creation of the bean
      NoSuchBeanException - if the bean doesn't exist
      NonUniqueBeanException - if more than one bean matching the current qualifier exists and cannot be resolved unambiguously
    • asJakartaProvider

      @NonNull default @NonNull Provider<T> asJakartaProvider()
      Convert this provider into a jakarta provider.
      Returns:
      The jakarta provider.
      Since:
      4.5.0
    • find

      default Optional<T> find(@Nullable @Nullable Qualifier<T> qualifier)
      Finds a bean for the optionally specified qualifier. Return empty if non-exists.
      Parameters:
      qualifier - The qualifier to use. Can be null which is equivalent to specifying the default qualifier.
      Returns:
      An optional of the bean.
      Since:
      3.2.0
    • getDefinition

      @NonNull default @NonNull BeanDefinition<T> getDefinition()
      Obtains a reference to the BeanDefinition if the bean is resolvable.
      Returns:
      The BeanDefinition
      Throws:
      NoSuchBeanException - if the bean doesn't exist
      NonUniqueBeanException - if more than one bean matching the current qualifier exists and cannot be resolved unambiguously
      UnsupportedOperationException - If the BeanProvider was obtained via other means other than dependency injection
      Since:
      3.2.0
    • get

      @NonNull default T get(@Nullable @Nullable Qualifier<T> qualifier)
      Parameters:
      qualifier - The qualifier to use, can be null.
      Returns:
      A fully-constructed and injected instance of BeanProvider.
      Throws:
      BeanCreationException - If an error occurs during the creation of the bean
      NoSuchBeanException - if the bean doesn't exist
      NonUniqueBeanException - if more than one bean matching the current qualifier exists and cannot be resolved unambiguously
      Since:
      3.0.0
      See Also:
    • iterator

      @NonNull default @NonNull Iterator<T> iterator()
      Specified by:
      iterator in interface Iterable<T>
    • stream

      default Stream<T> stream()

      When called, provides back a Stream of the beans available in this provider. If no beans are found, it returns an empty stream.

      Returns:
      a Stream representing the beans associated with this BeanProvider object
      Since:
      3.0.0
    • isUnique

      default boolean isUnique()

      Determines if more than one bean matches the specified type and qualifiers.

      Returns:
      true if only one bean matches.
      Since:
      3.0.0
    • isPresent

      default boolean isPresent()

      Determines if there is a bean that matches the required type and qualifiers.

      Returns:
      true if at least one bean matches.
      Since:
      3.0.0
    • isResolvable

      default boolean isResolvable()
      Is the bean resolvable using the get() or ifPresent(Consumer) methods.

      A bean is said to be resolvable when it is both unique (see isUnique()) and present (see isPresent())

      Returns:
      True if the bean is resolvable
      Since:
      3.0.0
    • ifPresent

      default void ifPresent(@NonNull @NonNull Consumer<T> consumer)
      Executes the given logic if the bean is present. Executes get() to obtain the bean which may result in a NonUniqueBeanException if the bean is not unique.
      Parameters:
      consumer - the consumer
      Throws:
      NonUniqueBeanException - if the bean is not unique
      Since:
      3.0.0
      See Also:
    • ifResolvable

      default void ifResolvable(@NonNull @NonNull Consumer<T> consumer)
      Executes the given logic if the bean is resolvable. See isResolvable().
      Parameters:
      consumer - the consumer
      Since:
      3.0.0
      See Also:
    • orElse

      @Nullable default T orElse(@Nullable T alternative)
      Allows selecting an alternative bean if the backing bean is not present.
      Parameters:
      alternative - The alternative, can be null
      Returns:
      The bean if present or else the supplied alternative
      Since:
      3.0.2
    • argumentOf

      @NonNull static <T1> @NonNull Argument<BeanProvider<T1>> argumentOf(@NonNull @NonNull Class<T1> type)
      Create an argument for the given type to aid with bean provider lookup.
      Type Parameters:
      T1 - The generic type
      Parameters:
      type - The type
      Returns:
      3.0.0