Card 07/ 07
Recap: Bean Scopes and Lifecycle Callbacks
Singleton and prototype, shared state, clean-up, and scoped proxies, with where each was.
Scopes and lifecycle callbacks come down to the five ideas below, each listed with the card that covers it.
| The idea | Where |
|---|---|
| Singleton is one shared instance; prototype is a new one per request for the bean | Bean Scopes: Singleton and Prototype |
Two getBean calls on a prototype return two different objects | Bean Scopes Example: A Prototype Gives a New Instance Each Time |
| A changing field in a singleton is shared by every caller, and races under load | Mutable State in a Singleton Bean |
The container never calls @PreDestroy on a prototype; the caller cleans it up | @PostConstruct and @PreDestroy: Lifecycle Callbacks and Prototype Beans |
| A request-scoped bean needs a scoped proxy before a singleton can use it | Request-Scoped Beans in a Singleton: Why a Scoped Proxy Is Needed |
The question behind all five is when a bean is created and who holds it afterwards: answer that, and the right scope and the right clean-up follow.
← → move through sections and cards