SPRING CONCEPTS

Singleton and prototype scope

Spring Concepts / Singleton and prototype scope

Scope controls how the container reuses or creates instances for a bean definition.

Understand the idea

A singleton has one shared instance per definition per context. A prototype is created when that bean is resolved. Injecting a prototype directly into a singleton resolves it once; the singleton retains that reference. The prototype definition has not changed scope. Singleton scope does not provide thread safety.

A concrete example

@Scope("prototype")
public class ReportDraft { }

In the application

See the independently runnable examples in Stage 20, Stage 21, Stage 22.

Prototype does not mean one object per HTTP request.

See it in a working stage

Related concepts

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