It would be great if there was a variation of ObjectProvider.orderedStream() which provides not just the beans themselves, but the beans paired with their bean names.
Use case:
In writing frameworks for internal services, quite often it's important to pull in a series of beans of the same type, and ordering is important. We use orderedStream() for many situations, but there are a number which require the bean name as well.
While we can use getBeansOfType().entrySet().stream() from the application context, it's not as clean as using ObjectProvider.orderedStream(). Most importantly, I'd prefer from a clean code perspective to not inject GenericApplicationContext everywhere, but be explicit in the types we need by using ObjectProvider.
Comment From: blake-bauman
Is this a reasonable feature request?
Comment From: snicoll
Can you share why you can't use Map<String,T>
?
Comment From: blake-bauman
Are you referring to getBeansOfType()
, or something else? I mentioned the getBeansOfType()
Map in the description.
Comment From: snicoll
Yes I read that. I am asking why you can't inject the Map
directly rather than asking for support in ObjectProvider
.
Comment From: blake-bauman
Ahh, I see what you're saying now. We've had unreliable experiences using Maps/Collections as beans for injection. It worked most of the time, but not always. Which is why we turned to ObjectProvider in the first place, as it was reliable and repeatable.
In general, there's nothing you can do in ObjectProvider that you can't do somewhere else in some other way. The purpose of ObjectProvider (as I can tell) is that it's a cleaner, shorter route to accessing 1..N instances of specific bean types. The Javadoc says:
designed specifically for injection points, allowing for programmatic optionality and lenient not-unique handling
Which is why we love using ObjectProvider. While there are other ways of getting at all of these beans, the other ways generally rely on some sort of "magic" that has burned us in the past if not used exactly correctly. ObjectProvider removes any doubt about what you will get.
Comment From: rstoyanchev
Team Decision: to keep things simple, ObjectProvider
will remain focused on type based resolution and that should serve the common case. You can use getBeansOfType
for bean names. If there are specific issues with using that, please feel free to comment further and we can consider those separately..
Comment From: blake-bauman
The issue with using getBeansOfType() is the ordering. For example, if a bean is declared with @Ordered
and you want to get the available beans in the proper ordering. ObjectProvider.orderedStream()
preserves that ordering whereas getBeansOfType() does not guarantee the ordering. In the Javadoc, orderedStream() says it "will be" ordered, while getBeansOfType() says "should be" ordered "as far as possible"