The full picture
🎬 Spring bean lifecycle, end to end
1 / 81 · Instantiate: the container calls the constructor (resolving constructor args from other beans). Constructor injection happens here — which is why it guarantees required dependencies.
- Init trio order (memorize):
@PostConstruct→InitializingBean.afterPropertiesSet()→ XML/@Bean(initMethod=…). Same pattern mirrored for destruction. - Prefer annotations (
@PostConstruct/@PreDestroy, JSR-250) over the framework interfaces — no Spring import in your business class. - Prototype caveat: Spring never calls destruction callbacks for prototype beans — it hands them out and forgets. Cleanup is the caller's job.
- Why not do init work in the constructor? Dependencies aren't injected yet (for field/setter injection), proxies aren't applied, and
thismay leak.@PostConstructruns when the bean is fully populated.