Interview Prep Zoneby CuriouserLabs

Question 1 of 5 · senior level

Walk through the complete lifecycle of a Spring bean, from definition to destruction.

🎤 Say this first

Instantiate (constructor) → populate properties (@Autowired/@Value) → Aware callbacks → BeanPostProcessor before-init → init phase (@PostConstruct → afterPropertiesSet → init-method) → BeanPostProcessor after-init (AOP proxies created here) → in service → on shutdown: @PreDestroy → destroy() → destroy-method. The one fact to always land: the object placed in the context may be a proxy produced in the after-init step, not your raw instance.

The full picture

🎬 Spring bean lifecycle, end to end

1 / 8
ConstructorinstantiatePopulate@Autowired/@ValueAware*Aware callbacksBPPbefore-initInit@PostConstructBPPafter-init → proxy!Readysingleton cacheDestroy@PreDestroyGreen = completed phase. The after-init BeanPostProcessor is where @Transactional/@Async proxies are born.

1 · Instantiate: the container calls the constructor (resolving constructor args from other beans). Constructor injection happens here — which is why it guarantees required dependencies.

Eight stations from constructor to @PreDestroy — watch where the proxy is born.
  • Init trio order (memorize): @PostConstructInitializingBean.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 this may leak. @PostConstruct runs when the bean is fully populated.

🔄 Likely follow-up questions

  • In what order are @PostConstruct methods of different beans called? (dependency order, then no guarantee)
  • What's SmartLifecycle and when do you need its phases?
  • Why does @PreDestroy not run on kill -9, and what does that mean for cleanup design?