Interface ObjectProvider<T>
- Type Parameters:
T- the object type
- All Superinterfaces:
Iterable<T>,ObjectFactory<T>
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 Iterable.forEach(java.util.function.Consumer<? super T>)
iteration and allows for collection-style stream() access.
- Since:
- 4.3
- Author:
- Juergen Hoeller
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionReturn an instance (possibly shared or independent) of the object managed by this factory.default TgetIfAvailable(Supplier<T> defaultSupplier) Return an instance (possibly shared or independent) of the object managed by this factory.Return an instance (possibly shared or independent) of the object managed by this factory.default TgetIfUnique(Supplier<T> defaultSupplier) Return an instance (possibly shared or independent) of the object managed by this factory.Return an instance (possibly shared or independent) of the object managed by this factory.default voidifAvailable(Consumer<T> dependencyConsumer) Consume an instance (possibly shared or independent) of the object managed by this factory, if available.default voidConsume an instance (possibly shared or independent) of the object managed by this factory, if unique.iterator()Return anIteratorover all matching object instances, without specific ordering guarantees (but typically in registration order).Return a sequentialStreamover all matching object instances, pre-ordered according to the factory's common order comparator.stream()Return a sequentialStreamover all matching object instances, without specific ordering guarantees (but typically in registration order).Methods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface org.springframework.beans.factory.ObjectFactory
getObject
-
Method Details
-
getObject
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- arguments to use when creating a corresponding instance- Returns:
- an instance of the bean
- Throws:
BeansException- in case of creation errors- See Also:
-
getIfAvailable
Return an instance (possibly shared or independent) of the object managed by this factory.- Returns:
- an instance of the bean, or
nullif not available - Throws:
BeansException- in case of creation errors- See Also:
-
getIfAvailable
Return an instance (possibly shared or independent) of the object managed by this factory.- Parameters:
defaultSupplier- a callback for supplying a default object if none is present in the factory- Returns:
- an instance of the bean, or the supplied default object if no such bean is available
- Throws:
BeansException- in case of creation errors- Since:
- 5.0
- See Also:
-
ifAvailable
Consume an instance (possibly shared or independent) of the object managed by this factory, if available.- Parameters:
dependencyConsumer- a callback for processing the target object if available (not called otherwise)- Throws:
BeansException- in case of creation errors- Since:
- 5.0
- See Also:
-
getIfUnique
Return an instance (possibly shared or independent) of the object managed by this factory.- Returns:
- an instance of the bean, or
nullif not available or not unique (i.e. multiple candidates found with none marked as primary) - Throws:
BeansException- in case of creation errors- See Also:
-
getIfUnique
Return an instance (possibly shared or independent) of the object managed by this factory.- Parameters:
defaultSupplier- a callback for supplying a default object if no unique candidate is present in the factory- 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)
- Throws:
BeansException- in case of creation errors- Since:
- 5.0
- See Also:
-
ifUnique
Consume an instance (possibly shared or independent) of the object managed by this factory, if unique.- Parameters:
dependencyConsumer- a callback for processing the target object if unique (not called otherwise)- Throws:
BeansException- in case of creation errors- Since:
- 5.0
- See Also:
-
iterator
Return anIteratorover all matching object instances, without specific ordering guarantees (but typically in registration order). -
stream
Return a sequentialStreamover all matching object instances, without specific ordering guarantees (but typically in registration order).- Since:
- 5.1
- See Also:
-
orderedStream
Return a sequentialStreamover 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
Orderedconventions, and in case of annotation-based configuration also considering theOrderannotation, analogous to multi-element injection points of list/array type.- Since:
- 5.1
- See Also:
-