Scrivio Interview library

Bean scopes

21 questions in 7 subtopics.

Singleton

ConceptFoundations5 min read
What does singleton scope mean in Spring, and how is it different from the singleton design pattern?
ScenarioIntermediate6 min read
In tests, two cached Spring contexts exist in one JVM and a singleton cache bean seems to hold different data depending on the test class. Explain.
LeadershipPrincipal6 min read
Spring made the shared singleton the default unit of application code. What long-term effects did this have on how Java teams design services, and is it still the right default?

Prototype

ConceptFoundations5 min read
How does prototype scope behave, and what does the container not do for prototype beans?
DesignIntermediate6 min read
When would you choose prototype scope, and when is a plain 'new' or a factory method better?

Request and session

ConceptFoundations6 min read
What are the request and session scopes, and what happens if a request-scoped bean is used outside a web request?
DebuggingIntermediate7 min read
A request-scoped bean holding the current tenant id works in controllers but fails inside an @Async method called from a controller. Why, and what are the options?
DesignAdvanced6 min read
A web application stores a shopping cart in a session-scoped bean. You need to scale to many instances. What do you consider?
LeadershipPrincipal6 min read
Request and session scopes tie application code to the servlet request model. As your company adds messaging, batch jobs and async processing, what guidance would you give about using web scopes in shared code?

Scoped proxies

CodingFoundations6 min read
Show how to inject a request-scoped bean into a singleton controller, and explain why the proxy is needed.
InternalsIntermediate6 min read
How does a scoped proxy work inside, and what are its costs?
DesignAdvanced7 min read
A shared library injects a request-scoped 'CurrentUser' proxy into singletons across 50 services. Some services now run the same code from Kafka listeners and fail. How would you redesign it?

Prototype inside singleton

DebuggingFoundations6 min read
A singleton service injects a prototype-scoped ReportBuilder with @Autowired, expecting a new builder for each call, but data from one report appears in the next. Why, and how do you fix it?
CodingIntermediate6 min read
Show how @Lookup method injection works for getting a new prototype from a singleton. What are its limits?

Custom scopes

CodingIntermediate7 min read
How would you implement and register a custom Spring scope, for example a scope bound to a batch job execution?
DesignAdvanced7 min read
A team proposes a custom 'tenant' scope so that each tenant gets its own instance of several beans. Evaluate this design.
LeadershipPrincipal7 min read
Your multi-tenant platform has used thread-bound context and custom scopes for tenant data for ten years. With virtual threads and more async code, how would you rethink tenant context handling?

Thread safety of singletons

DebuggingFoundations7 min read
This singleton controller returns wrong results under load. Explain the bug:
@RestController
class PriceController {
private BigDecimal total;
@GetMapping("/sum")
BigDecimal sum(@RequestParam List<BigDecimal> items) {
total = BigDecimal.ZERO;
for (var i : items) total = total.add(i);
return total;
}
}
DesignIntermediate7 min read
Which kinds of state are acceptable in a Spring singleton, and how do you make shared state safe?
LeadershipAdvanced7 min read
How would you prevent thread-safety bugs in singletons across many teams' Spring services?
ScenarioPrincipal7 min read
A production incident shows customer data leaked between requests because a singleton bean cached the last user. How would you respond as the senior technical leader, both immediately and over the next year?