Package io.micronaut.context
Interface BeanProvider<T>
-
- Type Parameters:
T
- The generic bean type
- All Superinterfaces:
java.lang.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 java.lang.Iterable<T>
A BeanProvider is a richer replacement for theProvider
interface that provides additional Micronaut specific methods to assist in programmatic bean creation and discovery.- Since:
- 2.4.0
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <T1> io.micronaut.core.type.Argument<BeanProvider<T1>>
argumentOf(java.lang.Class<T1> type)
Create an argument for the given type to aid with bean provider lookup.default java.util.Optional<T>
find(Qualifier<T> qualifier)
Finds a bean for the optionally specified qualifier.T
get()
The get method will materialize an instance of the bean if it is resolvable.default T
get(Qualifier<T> qualifier)
default BeanDefinition<T>
getDefinition()
Obtains a reference to theBeanDefinition
if the bean is resolvable.default void
ifPresent(java.util.function.Consumer<T> consumer)
Executes the given logic if the bean is present.default void
ifResolvable(java.util.function.Consumer<T> consumer)
Executes the given logic if the bean is resolvable.default boolean
isPresent()
Determines if there is a bean that matches the required type and qualifiers.default boolean
isResolvable()
Is the bean resolvable using theget()
orifPresent(Consumer)
methods.default boolean
isUnique()
Determines if more than one bean matches the specified type and qualifiers.default java.util.Iterator<T>
iterator()
default T
orElse(T alternative)
Allows selecting an alternative bean if the backing bean is not present.default java.util.stream.Stream<T>
stream()
When called, provides back a Stream of the beans available in this provider.
-
-
-
Method Detail
-
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()
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
-
find
default java.util.Optional<T> find(@Nullable Qualifier<T> qualifier)
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
@NonNull default BeanDefinition<T> 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 unambiguouslyjava.lang.UnsupportedOperationException
- If the BeanProvider was obtained via other means other than dependency injection- Since:
- 3.2.0
-
get
@NonNull default T get(@Nullable Qualifier<T> qualifier)
- 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:
get()
-
iterator
@NonNull default java.util.Iterator<T> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<T>
-
stream
default java.util.stream.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 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
default void ifPresent(@NonNull java.util.function.Consumer<T> consumer)
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:
isPresent()
-
ifResolvable
default void ifResolvable(@NonNull java.util.function.Consumer<T> consumer)
Executes the given logic if the bean is resolvable. SeeisResolvable()
.- Parameters:
consumer
- the consumer- Since:
- 3.0.0
- See Also:
isResolvable()
-
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 benull
- Returns:
- The bean if present or else the supplied alternative
- Since:
- 3.0.2
-
argumentOf
@NonNull static <T1> io.micronaut.core.type.Argument<BeanProvider<T1>> argumentOf(@NonNull java.lang.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
-
-