Card 04/ 07
All 7 cards
ConceptDifficulty: Intermediate1 min
@PostConstruct, @PreDestroy, and Who Owns a Prototype's Cleanup
@PostConstruct marks a method the container runs once, right after a bean's dependencies are set — the initialize phase from the bean lifecycle. @PreDestroy marks one it runs on shutdown, before the bean is discarded.
Both work exactly as expected for a singleton. For a prototype, only half of that promise holds: the container creates it and hands it over, @PostConstruct runs — and then the container stops tracking it entirely. It does not know when you're done with a prototype bean, so @PreDestroy on one is never called by the container. Nothing is watching to call it.
closing context...
singleton receipt cleaned up
closed.From the moment a prototype bean is handed over, cleaning it up is the caller's job, the same as it would be for any object you constructed with new yourself.