Quick revision
The short answer
ObjectProvider resolves the dependency lazily, at the moment we call getObject() or getIfAvailable(), instead of at construction. It can return a bean only when exactly one candidate exists with getIfUnique(), iterate over all candidates with stream() and orderedStream(), and fetch a new prototype instance on each call. Because nothing is resolved up front, it avoids startup failures when the bean is absent or ambiguous.
What to remember
In this answer, we saw what ObjectProvider can do that a direct injection cannot. It resolves lazily, when we call getObject() or getIfAvailable(), so absent or late beans do not break startup. getIfUnique() returns a bean only when the choice is unambiguous, stream() and orderedStream() iterate over all candidates even when there are none, and getObject() on a prototype gives a new instance every time. Those properties make it the right tool for optional, ambiguous, multiple and prototype dependencies, while required dependencies stay as plain constructor parameters.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
ObjectProvider resolves the dependency lazily, at the moment we call getObject() or getIfAvailable(), instead of at construction. It can return a bean only when exactly one candidate exists with getIfUnique(), iterate over all candidates with stream() and orderedStream(), and fetch a new prototype instance on each call. Because nothing is resolved up front, it avoids startup failures when the bean is absent or ambiguous.
Why this question is asked
A direct injection, a constructor parameter of type MetricsPublisher, is a simple contract: one bean, resolved once, when the object is created. Most of the time that is exactly right. ObjectProvider exists for the cases where that contract is too rigid, and it shows up all over Spring Boot's own auto-configuration. Interviewers ask about it to see whether we know those cases and can use the right method for each. In this answer, we will recap what a direct injection promises, then go through each capability of ObjectProvider with a small example: lazy resolution, absence, uniqueness, streams and prototypes.
1A Quick Recap: What a Direct Injection Promises
When a constructor asks for a MetricsPublisher, Spring must find exactly one matching bean before it can create our object. If there is none, startup fails with a missing bean error. If there are two and neither is primary, startup fails with NoUniqueBeanDefinitionException. And the bean is fetched once: whatever we got at construction is what we keep, even if it is a prototype, a bean scope where every lookup should create a new instance.
ObjectProvider<T> changes that contract. Spring injects a small handle instead of the bean, and the handle looks up beans of type T whenever we ask it to.
2Lazy Resolution
Nothing is resolved when the handle is injected. The lookup happens at the moment we call one of its methods.
This helps in two ways. An expensive bean marked @Lazy is only built when it is actually needed. And it lets us depend on a bean that would otherwise create an ordering problem at startup, because by the time we call getObject() the context is fully started. getObject() behaves like a direct injection at call time: it throws if the bean is missing or ambiguous, so we use it when we expect the bean to be there.
3Absence Without Failure
getIfAvailable() returns the bean, or null if no bean of that type exists, and has variants that take a default supplier or a consumer.
This is the reason Spring Boot's auto-configurations use ObjectProvider so heavily: a starter cannot know whether the application defines a MetricsPublisher, and it must not fail if it does not.
4Only When Unique: getIfUnique()
getIfUnique() returns the bean only if there is exactly one candidate, or a primary among several. If there are none, or several with no primary, it returns null instead of failing.
Notice the difference between the two methods: getIfAvailable() tolerates absence but not ambiguity, while getIfUnique() tolerates both. getIfUnique() suits code that should use a bean only when the choice is obvious, for example a library that picks up an application's single Clock bean but falls back to the system clock if the application has none or several.
5All Candidates: stream() and orderedStream()
When we want every bean of a type, stream() returns them all and orderedStream() returns them sorted by @Order, the Ordered interface or @Priority.
A List<RowValidator> injection gives the same ordering, but it fails at startup if there are no validators at all, while the stream is simply empty. The stream is also built at call time, so it reflects the context as it is when we use it.
6A New Prototype Each Time
For a prototype-scoped bean, a direct injection creates one instance when our singleton is built and keeps it forever. With ObjectProvider, each getObject() call asks the container again, so each call creates a new instance.
| Situation | Direct injection | ObjectProvider |
|---|---|---|
| Bean absent | startup fails | getIfAvailable() null or default |
| Several beans, no primary | startup fails | getIfUnique() null; stream() all |
| When resolved | at construction | at each call |
| Prototype bean | one instance kept forever | new instance per getObject() |
| Zero or more beans in order | List fails if empty | orderedStream() may be empty |
This is the clean answer to the classic question of how a singleton can use a stateful prototype safely: ask for a new one each time instead of holding one. getObject(Object... args) can even pass constructor arguments to the prototype.
7Follow-up questions
Is ObjectProvider the same as javax.inject.Provider or jakarta.inject.Provider?
They solve the same core problem, lazy lookup and a new prototype per get(), and Spring supports jakarta.inject.Provider<T> at injection points too. ObjectProvider extends Spring's ObjectFactory and adds the Spring-specific methods: getIfAvailable, getIfUnique, ifAvailable, stream and orderedStream. Use the Jakarta Provider if you want to avoid Spring types; use ObjectProvider when you need the extra methods.
Does ObjectProvider respect @Qualifier?
Yes. A qualifier on the ObjectProvider parameter, for example @Qualifier("reporting") ObjectProvider<DataSource>, restricts every lookup through that handle to the qualified beans.
Are there downsides to using ObjectProvider everywhere?
Yes. It hides the real dependency behind a handle, moves failures from startup to the first call, and adds a Spring type to the class. For a dependency that must always exist, a direct injection is clearer and fails fast. ObjectProvider is for the specific cases: optional, ambiguous, many, lazy or prototype.
8Conclusion
In this answer, we saw what ObjectProvider can do that a direct injection cannot. It resolves lazily, when we call getObject() or getIfAvailable(), so absent or late beans do not break startup. getIfUnique() returns a bean only when the choice is unambiguous, stream() and orderedStream() iterate over all candidates even when there are none, and getObject() on a prototype gives a new instance every time. Those properties make it the right tool for optional, ambiguous, multiple and prototype dependencies, while required dependencies stay as plain constructor parameters.
Open with
A direct injection means exactly one bean, resolved once, when my object is constructed; if it is missing or ambiguous, startup fails. ObjectProvider of T changes that contract: Spring injects a handle, and the lookup happens whenever I call it.
Then, if they want more
That gives me several things. First, laziness: getObject or getIfAvailable resolves the bean at call time, so a lazy or expensive bean is built only when needed, and the context is fully started by then.
Second, absence: getIfAvailable returns null, or a default if I pass a supplier, and ifAvailable runs code only when the bean exists, so a missing bean is not a startup failure. That is why Spring Boot's auto-configurations use ObjectProvider so much.
Third, uniqueness: getIfUnique returns the bean only when there is exactly one candidate or a primary, and returns null instead of throwing when there are several.
Fourth, many beans: stream and orderedStream give me all candidates, sorted by @Order or Ordered in the second case, and an empty stream if there are none, while injecting a List would fail when empty. Fifth, prototypes: each getObject on a prototype-scoped bean creates a new instance, which is the clean way for a singleton to use a stateful prototype.
I still use plain constructor injection for required dependencies, because it fails fast and is clearer.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What can ObjectProvider do that a direct injection cannot?": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/773353deea/