T
- The generic bean type@FunctionalInterface public interface BeanProvider<T> extends Iterable<T>
Provider
interface that
provides additional Micronaut specific methods to assist in programmatic bean creation and discovery.Modifier and Type | Method and Description |
---|---|
static <T1> Argument<BeanProvider<T1>> |
argumentOf(Class<T1> type)
Create an argument for the given type to aid with bean provider lookup.
|
default 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 the
BeanDefinition if the bean is resolvable. |
default void |
ifPresent(Consumer<T> consumer)
Executes the given logic if the bean is present.
|
default void |
ifResolvable(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 the
get() or ifPresent(Consumer) methods. |
default boolean |
isUnique()
Determines if more than one bean matches the specified type and qualifiers.
|
default Iterator<T> |
iterator() |
default T |
orElse(T alternative)
Allows selecting an alternative bean if the backing bean is not present.
|
default Stream<T> |
stream()
When called, provides back a Stream of the beans available in this provider.
|
forEach, spliterator
@NonNull T get()
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.
T
.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 unambiguouslydefault Optional<T> find(@Nullable Qualifier<T> qualifier)
qualifier
- The qualifier to use. Can be null
which is equivalent to specifying the default qualifier.@NonNull default BeanDefinition<T> getDefinition()
BeanDefinition
if the bean is resolvable.BeanDefinition
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@NonNull default T get(@Nullable Qualifier<T> qualifier)
qualifier
- The qualifier to use, can be null
.T
.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 unambiguouslyget()
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.
Stream
representing the beans associated with this BeanProvider
objectdefault boolean isUnique()
Determines if more than one bean matches the specified type and qualifiers.
>true
if only one bean matches.default boolean isPresent()
Determines if there is a bean that matches the required type and qualifiers.
default boolean isResolvable()
get()
or ifPresent(Consumer)
methods.
A bean is said to be resolvable when it is both unique (see isUnique()
) and present (see isPresent()
)
default void ifPresent(@NonNull Consumer<T> consumer)
get()
to obtain the bean which may result in a NonUniqueBeanException
if the bean is not unique.consumer
- the consumerNonUniqueBeanException
- if the bean is not uniqueisPresent()
default void ifResolvable(@NonNull Consumer<T> consumer)
isResolvable()
.consumer
- the consumerisResolvable()
@Nullable default T orElse(@Nullable T alternative)
alternative
- The alternative, can be null
@NonNull static <T1> Argument<BeanProvider<T1>> argumentOf(@NonNull Class<T1> type)
T1
- The generic typetype
- The type