Loading published curriculum…

09 · Spring Framework and the IoC container / LESSON 41 OF 102

Singleton, prototype, and request scopes

Bean scope defines instance lifetime; it does not automatically make an object thread-safe.

Before this lesson: Bean ambiguity, @Qualifier, and @Primary

Understand the topic

Singleton means one instance per bean definition per container. Web requests can call that same instance concurrently, so store user-specific state in parameters or request-scoped objects. Prototype creates instances on lookup, but injecting one prototype into a singleton happens only when the singleton is created.

Use ObjectProvider for deliberate repeated prototype lookup. Request scope requires a web request context; background workers do not have one by default. Prototype destruction is not managed in the same way as singleton shutdown.

EXAMPLE · JAVA

CounterConfig.java

import org.springframework.context.annotation.*;
import java.util.concurrent.atomic.AtomicInteger;
@Configuration
public class CounterConfig {
 @Bean @Scope("prototype")
 AtomicInteger counter() { return new AtomicInteger(); }
}

What to expect

Two context.getBean(AtomicInteger.class) lookups return different counters; one constructor injection resolves only once.

YOUR TURN

Put it into practice

  1. 01Compare singleton and prototype object identities across lookups.
  2. 02Inject a prototype into a singleton and then repeat using ObjectProvider.getObject.
  3. 03Verify: You can demonstrate exactly when a new instance is created in each case.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

Bean scope defines instance lifetime; it does not automatically make an object thread-safe.

  1. 01Singleton means one instance per bean definition per container. Web requests can call that same instance concurrently, so store user-specific state in parameters or request-scoped objects. Prototype creates instances on lookup, but injecting one prototype into a singleton happens only when the singleton is created.
  2. 02Use ObjectProvider for deliberate repeated prototype lookup. Request scope requires a web request context; background workers do not have one by default. Prototype destruction is not managed in the same way as singleton shutdown.
  3. 03Trace the example: Two context.getBean(AtomicInteger.class) lookups return different counters; one constructor injection resolves only once.

A mistake worth avoiding

Two users see the same mutable request data.

DEBUGGING ROOM

Something broke. Where do you look?

Two users see the same mutable request data.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

Does singleton scope imply thread safety?

Choose one answer

BUILD IT YOURSELF

Compare singleton and prototype object identities across lookups.

  1. 1.Compare singleton and prototype object identities across lookups.
  2. 2.Inject a prototype into a singleton and then repeat using ObjectProvider.getObject.
  3. 3.Reproduce this failure, then repair it: Two users see the same mutable request data.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

Does singleton scope imply thread safety?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

You can demonstrate exactly when a new instance is created in each case.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references