SPRING CONCEPTS

On-demand bean resolution

Spring Concepts / On-demand bean resolution

A provider lets a consumer request a bean when an operation needs it.

Understand the idea

ObjectProvider extends ObjectFactory. Calling getObject resolves the configured bean at that point. With prototype scope each resolution produces a fresh instance; with singleton scope the same instance is returned. The provider itself does not force a scope change.

A concrete example

ReportDraft draft = drafts.getObject();

In the application

See the independently runnable examples in Stage 22.

Injecting a provider and injecting one resolved draft are different dependencies.

See it in a working stage

Related concepts

Read the official documentation ↗
Java 21 · Spring Boot 4.1.1 · Edupoly