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.