Package io.micronaut.context
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.
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 Summary
Modifier and TypeMethodDescriptionstatic <T1> @NonNull Argument<BeanProvider<T1>>
argumentOf
(@NonNull Class<T1> type) Create an argument for the given type to aid with bean provider lookup.Convert this provider into a jakarta provider.Finds a bean for the optionally specified qualifier.get()
The get method will materialize an instance of the bean if it is resolvable.default T
default @NonNull BeanDefinition<T>
Obtains a reference to theBeanDefinition
if the bean is resolvable.default void
Executes the given logic if the bean is present.default void
ifResolvable
(@NonNull Consumer<T> consumer) Executes the given logic if the bean is resolvable.default boolean
Determines if there is a bean that matches the required type and qualifiers.default boolean
Is the bean resolvable using theget()
orifPresent(Consumer)
methods.default boolean
isUnique()
Determines if more than one bean matches the specified type and qualifiers.iterator()
default T
Allows selecting an alternative bean if the backing bean is not present.stream()
When called, provides back a Stream of the beans available in this provider.Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
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()
andisPresent()
.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 beanNoSuchBeanException
- if the bean doesn't existNonUniqueBeanException
- if more than one bean matching the current qualifier exists and cannot be resolved unambiguously
-
asJakartaProvider
Convert this provider into a jakarta provider.- Returns:
- The jakarta provider.
- Since:
- 4.5.0
-
find
Finds a bean for the optionally specified qualifier. Return empty if non-exists.- Parameters:
qualifier
- The qualifier to use. Can benull
which is equivalent to specifying the default qualifier.- Returns:
- An optional of the bean.
- Since:
- 3.2.0
-
getDefinition
Obtains a reference to theBeanDefinition
if the bean is resolvable.- Returns:
- The
BeanDefinition
- Throws:
NoSuchBeanException
- if the bean doesn't existNonUniqueBeanException
- if more than one bean matching the current qualifier exists and cannot be resolved unambiguouslyUnsupportedOperationException
- If the BeanProvider was obtained via other means other than dependency injection- Since:
- 3.2.0
-
get
- Parameters:
qualifier
- The qualifier to use, can benull
.- Returns:
- A fully-constructed and injected instance of
BeanProvider
. - Throws:
BeanCreationException
- If an error occurs during the creation of the beanNoSuchBeanException
- if the bean doesn't existNonUniqueBeanException
- if more than one bean matching the current qualifier exists and cannot be resolved unambiguously- Since:
- 3.0.0
- See Also:
-
iterator
-
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 thisBeanProvider
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 theget()
orifPresent(Consumer)
methods.A bean is said to be resolvable when it is both unique (see
isUnique()
) and present (seeisPresent()
)- Returns:
- True if the bean is resolvable
- Since:
- 3.0.0
-
ifPresent
Executes the given logic if the bean is present. Executesget()
to obtain the bean which may result in aNonUniqueBeanException
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
Executes the given logic if the bean is resolvable. SeeisResolvable()
.- Parameters:
consumer
- the consumer- Since:
- 3.0.0
- See Also:
-
orElse
Allows selecting an alternative bean if the backing bean is not present.- Parameters:
alternative
- The alternative, can benull
- 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
-