Top Description Methods
org.springframework.beans.factory

public Interface ObjectProvider<T>

extends ObjectFactory<T>, Iterable<T>
Known Direct Subinterfaces
org.springframework.beans.factory.support.DefaultListableBeanFactory.BeanObjectProvider
Type Parameters
<T>
the object type
Imports
java.util.Iterator, java.util.function.Consumer, .Supplier, java.util.stream.Stream, org.springframework.beans.BeansException, org.springframework.lang.Nullable

A variant of ObjectFactory designed specifically for injection points, allowing for programmatic optionality and lenient not-unique handling.

As of 5.1, this interface extends Iterable and provides Stream support. It can be therefore be used in for loops, provides forEach iteration and allows for collection-style stream access.

Author
Juergen Hoeller
Since
4.3
See Also
BeanFactory#getBeanProvider, org.springframework.beans.factory.annotation.Autowired

Method Summary

Modifier and TypeMethod and Description
public T

Returns:

an instance of the bean, or null if not available
getIfAvailable
()

Return an instance (possibly shared or independent) of the object managed by this factory.

public default T

Returns:

an instance of the bean, or the supplied default object if no such bean is available
getIfAvailable
(Supplier<T>
a callback for supplying a default object if none is present in the factory
defaultSupplier
)

Return an instance (possibly shared or independent) of the object managed by this factory.

public T

Returns:

an instance of the bean, or null if not available or not unique (i.e. multiple candidates found with none marked as primary)
getIfUnique
()

Return an instance (possibly shared or independent) of the object managed by this factory.

public default T

Returns:

an instance of the bean, or the supplied default object if no such bean is available or if it is not unique in the factory (i.e. multiple candidates found with none marked as primary)
getIfUnique
(Supplier<T>
a callback for supplying a default object if no unique candidate is present in the factory
defaultSupplier
)

Return an instance (possibly shared or independent) of the object managed by this factory.

public T

Returns:

an instance of the bean
getObject
(Object...
arguments to use when creating a corresponding instance
args
)

Return an instance (possibly shared or independent) of the object managed by this factory.

public default void
ifAvailable(Consumer<T>
a callback for processing the target object if available (not called otherwise)
dependencyConsumer
)

Consume an instance (possibly shared or independent) of the object managed by this factory, if available.

public default void
ifUnique(Consumer<T>
a callback for processing the target object if unique (not called otherwise)
dependencyConsumer
)

Consume an instance (possibly shared or independent) of the object managed by this factory, if unique.

public default Iterator<T>
iterator()

Implements java.lang.Iterable.iterator.

Return an Iterator over all matching object instances, without specific ordering guarantees (but typically in registration order).

public default Stream<T>
orderedStream()

Return a sequential Stream over all matching object instances, pre-ordered according to the factory's common order comparator.

public default Stream<T>
stream()

Return a sequential Stream over all matching object instances, without specific ordering guarantees (but typically in registration order).

Inherited from java.lang.Iterable:
forEachspliterator
Inherited from org.springframework.beans.factory.ObjectFactory:
getObject

Method Detail

getIfAvailableback to summary
public T getIfAvailable() throws BeansException

Return an instance (possibly shared or independent) of the object managed by this factory.

Returns:T

an instance of the bean, or null if not available

Annotations
@Nullable
Exceptions
BeansException:
in case of creation errors
See Also
getObject()
getIfAvailableback to summary
public default T getIfAvailable(Supplier<T> defaultSupplier) throws BeansException

Return an instance (possibly shared or independent) of the object managed by this factory.

Parameters
defaultSupplier:Supplier<T>

a callback for supplying a default object if none is present in the factory

Returns:T

an instance of the bean, or the supplied default object if no such bean is available

Exceptions
BeansException:
in case of creation errors
Since
5.0
See Also
getIfAvailable()
getIfUniqueback to summary
public T getIfUnique() throws BeansException

Return an instance (possibly shared or independent) of the object managed by this factory.

Returns:T

an instance of the bean, or null if not available or not unique (i.e. multiple candidates found with none marked as primary)

Annotations
@Nullable
Exceptions
BeansException:
in case of creation errors
See Also
getObject()
getIfUniqueback to summary
public default T getIfUnique(Supplier<T> defaultSupplier) throws BeansException

Return an instance (possibly shared or independent) of the object managed by this factory.

Parameters
defaultSupplier:Supplier<T>

a callback for supplying a default object if no unique candidate is present in the factory

Returns:T

an instance of the bean, or the supplied default object if no such bean is available or if it is not unique in the factory (i.e. multiple candidates found with none marked as primary)

Exceptions
BeansException:
in case of creation errors
Since
5.0
See Also
getIfUnique()
getObjectback to summary
public T getObject(Object... args) throws BeansException

Return an instance (possibly shared or independent) of the object managed by this factory.

Allows for specifying explicit construction arguments, along the lines of BeanFactory#getBean(String, Object...).

Parameters
args:Object[]

arguments to use when creating a corresponding instance

Returns:T

an instance of the bean

Exceptions
BeansException:
in case of creation errors
See Also
getObject()
ifAvailableback to summary
public default void ifAvailable(Consumer<T> dependencyConsumer) throws BeansException

Consume an instance (possibly shared or independent) of the object managed by this factory, if available.

Parameters
dependencyConsumer:Consumer<T>

a callback for processing the target object if available (not called otherwise)

Exceptions
BeansException:
in case of creation errors
Since
5.0
See Also
getIfAvailable()
ifUniqueback to summary
public default void ifUnique(Consumer<T> dependencyConsumer) throws BeansException

Consume an instance (possibly shared or independent) of the object managed by this factory, if unique.

Parameters
dependencyConsumer:Consumer<T>

a callback for processing the target object if unique (not called otherwise)

Exceptions
BeansException:
in case of creation errors
Since
5.0
See Also
getIfAvailable()
iteratorback to summary
public default Iterator<T> iterator()

Implements java.lang.Iterable.iterator.

Return an Iterator over all matching object instances, without specific ordering guarantees (but typically in registration order).

Returns:Iterator<T>

Doc from java.lang.Iterable.iterator.

an Iterator.

Annotations
@Override
Since
5.1
See Also
stream()
orderedStreamback to summary
public default Stream<T> orderedStream()

Return a sequential Stream over all matching object instances, pre-ordered according to the factory's common order comparator.

In a standard Spring application context, this will be ordered according to org.springframework.core.Ordered conventions, and in case of annotation-based configuration also considering the org.springframework.core.annotation.Order annotation, analogous to multi-element injection points of list/array type.

Since
5.1
See Also
stream(), org.springframework.core.OrderComparator
streamback to summary
public default Stream<T> stream()

Return a sequential Stream over all matching object instances, without specific ordering guarantees (but typically in registration order).

Since
5.1
See Also
iterator(), orderedStream()