Card 07/ 07

RecapDifficulty: Advanced1 min

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.

What the topic established, and which card it was on
The ideaWhere
Singleton is one shared instance; prototype is a new one per request for the beanBean Scopes: Singleton and Prototype
Two getBean calls on a prototype return two different objectsBean Scopes Example: A Prototype Gives a New Instance Each Time
A changing field in a singleton is shared by every caller, and races under loadMutable 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 itRequest-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.