T
- the object typepublic interface ObjectProvider<T> extends ObjectFactory<T>, java.lang.Iterable<T>
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 Iterable.forEach(java.util.function.Consumer<? super T>)
iteration and allows for collection-style stream()
access.
BeanFactory.getBeanProvider(java.lang.Class<T>)
,
Autowired
Modifier and Type | Method and Description |
---|---|
T |
getIfAvailable()
Return an instance (possibly shared or independent) of the object
managed by this factory.
|
default T |
getIfAvailable(java.util.function.Supplier<T> defaultSupplier)
Return an instance (possibly shared or independent) of the object
managed by this factory.
|
T |
getIfUnique()
Return an instance (possibly shared or independent) of the object
managed by this factory.
|
default T |
getIfUnique(java.util.function.Supplier<T> defaultSupplier)
Return an instance (possibly shared or independent) of the object
managed by this factory.
|
T |
getObject(java.lang.Object... args)
Return an instance (possibly shared or independent) of the object
managed by this factory.
|
default void |
ifAvailable(java.util.function.Consumer<T> dependencyConsumer)
Consume an instance (possibly shared or independent) of the object
managed by this factory, if available.
|
default void |
ifUnique(java.util.function.Consumer<T> dependencyConsumer)
Consume an instance (possibly shared or independent) of the object
managed by this factory, if unique.
|
default java.util.Iterator<T> |
iterator()
Return an
Iterator over all matching object instances,
without specific ordering guarantees (but typically in registration order). |
default java.util.stream.Stream<T> |
orderedStream()
Return a sequential
Stream over all matching object instances,
pre-ordered according to the factory's common order comparator. |
default java.util.stream.Stream<T> |
stream()
Return a sequential
Stream over all matching object instances,
without specific ordering guarantees (but typically in registration order). |
getObject
T getObject(java.lang.Object... args) throws BeansException
Allows for specifying explicit construction arguments, along the
lines of BeanFactory.getBean(String, Object...)
.
args
- arguments to use when creating a corresponding instanceBeansException
- in case of creation errorsObjectFactory.getObject()
@Nullable T getIfAvailable() throws BeansException
null
if not availableBeansException
- in case of creation errorsObjectFactory.getObject()
default T getIfAvailable(java.util.function.Supplier<T> defaultSupplier) throws BeansException
defaultSupplier
- a callback for supplying a default object
if none is present in the factoryBeansException
- in case of creation errorsgetIfAvailable()
default void ifAvailable(java.util.function.Consumer<T> dependencyConsumer) throws BeansException
dependencyConsumer
- a callback for processing the target object
if available (not called otherwise)BeansException
- in case of creation errorsgetIfAvailable()
@Nullable T getIfUnique() throws BeansException
null
if not available or
not unique (i.e. multiple candidates found with none marked as primary)BeansException
- in case of creation errorsObjectFactory.getObject()
default T getIfUnique(java.util.function.Supplier<T> defaultSupplier) throws BeansException
defaultSupplier
- a callback for supplying a default object
if no unique candidate is present in the factoryBeansException
- in case of creation errorsgetIfUnique()
default void ifUnique(java.util.function.Consumer<T> dependencyConsumer) throws BeansException
dependencyConsumer
- a callback for processing the target object
if unique (not called otherwise)BeansException
- in case of creation errorsgetIfAvailable()
default java.util.Iterator<T> iterator()
Iterator
over all matching object instances,
without specific ordering guarantees (but typically in registration order).default java.util.stream.Stream<T> stream()
Stream
over all matching object instances,
without specific ordering guarantees (but typically in registration order).iterator()
,
orderedStream()
default java.util.stream.Stream<T> orderedStream()
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 Ordered
conventions,
and in case of annotation-based configuration also considering the
Order
annotation,
analogous to multi-element injection points of list/array type.
stream()
,
OrderComparator