Ch. 8 · Spring Boot

Spring Bean Scopes and Shared Mutable State

Spring Bean Scopes and Shared Mutable State. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readbeginnerupdated Oct 3, 2026

Singleton scope normally means one instance per application context. Shared mutable request-specific fields can race between concurrent requests.

Before you start

You should know Java classes, dependency injection and basic HTTP requests. Identify where a call crosses a framework-managed boundary. The snippets illustrate a focused mechanism; database configuration, application wiring and authentication must be supplied by the surrounding application when applicable.

The practical goal is to reason through this situation: A singleton service should not store the current user’s request in an instance field. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Identify shared instances

A singleton bean is shared within its application context across concurrent requests.

Step 2: Remove request-specific fields

Pass user and request data as parameters or through an intentional scoped abstraction.

Step 3: Check concurrent behavior

Trace two requests interleaving access rather than testing one request alone.

Worked scenario

A singleton service should not store the current user’s request in an instance field.

Request A sets a service field to user A. Request B overwrites it before A uses it, so A’s operation can act on B’s data. A stateless operation receiving user explicitly avoids this race. Separate application contexts and processes still have separate singleton instances.

Common mistake

Singleton does not mean one global instance across every process or context.

Verify the behavior

Interleave requests with different users and verify no context leaks between them.

Interview exercise

Store request data safely.

Answer and reasoning

Pass it through operation parameters or a deliberate request-scoped abstraction, keeping shared services stateless where practical.

Continue learning

Compare the scenario with the Spring Boot interview questions and test your understanding with the Spring Boot MCQs. For terminology and implementation details, consult the reference material.

More in Spring Boot

read ✓Spring Boot · hard

Spring @Async and Executor Configuration

Run methods asynchronously with @Async, configure a bounded executor, and handle exceptions and the proxy boundary.

~2 min readread →
read ✓Spring Boot · hard

Spring Boot Caching Abstraction

Cache method results with @Cacheable, choose keys and TTLs, and evict on writes without the self-invocation trap.

~2 min readread →
read ✓Spring Boot · hard

Spring Declarative HTTP Clients

Define outbound HTTP as an annotated interface with @HttpExchange, create the proxy, and configure timeouts and errors.

~2 min readread →
esc