Bean Scopes and the Bean Lifecycle
Spring beans are singletons by default, but there are prototype, request and session scopes too. Interviewers ask about the bean lifecycle, @PostConstruct, and the classic bug of injecting a prototype into a singleton.
What you will learn
- Name the bean scopes
- Walk through the bean lifecycle
- Inject shorter-lived beans into singletons correctly
- Know why singleton beans must be thread-safe
Key concepts
Scopes
singleton (one per container, default), prototype (new instance every time it is requested), request, session and application (web scopes), plus websocket.
Lifecycle
Instantiate → inject dependencies → Aware callbacks → BeanPostProcessor before-init → @PostConstruct / afterPropertiesSet / init-method → post-processor after-init (proxies for AOP are created here) → in use → @PreDestroy / destroy on shutdown.
Singleton thread-safety
One instance serves all requests concurrently, so do not keep request data in fields. Keep singletons stateless or use thread-safe structures.
Step by step
Prototype inside a singleton
A prototype injected into a singleton is created once, when the singleton is built, so you always get the same instance. Ask for a new one each time with ObjectProvider, @Lookup or a scoped proxy.
@Service
class ReportService {
private final ObjectProvider<ReportBuilder> builders; // ReportBuilder is @Scope("prototype")
ReportService(ObjectProvider<ReportBuilder> builders) { this.builders = builders; }
Report create() {
ReportBuilder b = builders.getObject(); // a fresh instance each call
return b.build();
}
}Examples
Lifecycle hooks
@Component
class CacheWarmer {
@PostConstruct
void warm() { /* runs after injection, before the bean is used */ }
@PreDestroy
void flush() { /* runs on graceful shutdown (not for prototype beans) */ }
}
// To run after the whole app has started:
@EventListener(ApplicationReadyEvent.class)
void onReady() { ... }Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Show the bug of a prototype inside a singleton, then fix it with ObjectProvider.
- Find a thread-safety bug in a singleton that stores the current user in a field.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
Is a Spring singleton the same as the Singleton pattern?
No. The GoF Singleton is one instance per class loader enforced by the class itself. A Spring singleton is one instance per ApplicationContext for that bean definition; you can define the same class as two beans and get two instances.
Does Spring call @PreDestroy on prototype beans?
No. Spring creates and hands over prototype beans but does not manage the rest of their lifecycle, so destroy callbacks are not called. The caller must clean them up.
Summary
Beans are singletons by default and must be stateless or thread-safe; know the lifecycle order, use @PostConstruct and @PreDestroy, and get fresh prototypes through ObjectProvider.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus