Java and Spring Level 3 This level · 44 of 56
Hard java spring

What is the complete lifecycle of a Spring Bean?

The 30 second answer

For a typical singleton, Spring constructs the object, resolving constructor dependencies, then injects fields and setters. It calls Aware callbacks and runs initialization post-processors, including the one that invokes @PostConstruct. Next come InitializingBean's afterPropertiesSet and any custom init method, followed by post-processors that can wrap the bean in an AOP proxy. On context shutdown, Spring runs @PreDestroy, DisposableBean's destroy callback and any custom destroy method. Prototype beans don't get automatic destruction callbacks.

Could you say this in an interview?

The core order is in AbstractAutowireCapableBeanFactory.doCreateBean:

instanceWrapper = createBeanInstance(beanName, mbd, args);   // 1. instantiate
...
populateBean(beanName, mbd, instanceWrapper);                 // 2. inject
exposedObject = initializeBean(beanName, exposedObject, mbd);  // 3. initialize
...
registerDisposableBeanIfNecessary(beanName, bean, mbd);       // 4. register destroy

AbstractAutowireCapableBeanFactory.java L565, L602-L603, L641

And initializeBean shows the initialization order:

invokeAwareMethods(beanName, bean);
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
    wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
invokeInitMethods(beanName, wrappedBean, mbd);
if (mbd == null || !mbd.isSynthetic()) {
    wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;

AbstractAutowireCapableBeanFactory.initializeBean L1797-L1822

Full order:

  1. Bean definition is registered (from scanning, @Bean or auto-configuration). BeanFactoryPostProcessors can change definitions before any bean exists.
  2. Instantiation: constructor call; constructor injection resolves dependencies here.
  3. Population: @Autowired fields and setters, @Value.
  4. Aware callbacks: BeanNameAware, BeanClassLoaderAware, BeanFactoryAware. (ApplicationContextAware and similar are handled by a post-processor in step 5.)
  5. **postProcessBeforeInitialization**: CommonAnnotationBeanPostProcessor calls @PostConstruct here.
  6. Init: InitializingBean.afterPropertiesSet(), then @Bean(initMethod = ...).
  7. **postProcessAfterInitialization**: AOP proxies for @Transactional, @Async, @Cacheable are created here. The object stored in the container may be a proxy, not your instance.
  8. Ready: after all singletons exist, SmartInitializingSingleton.afterSingletonsInstantiated() runs; SmartLifecycle beans start.
  9. Destroy (on context close): @PreDestroy, DisposableBean.destroy(), custom destroy method.

Scope matters: prototype beans go through steps 1–7 each time, but the container does not call their destroy callbacks.

Classic follow-up: why does @Transactional not work on a self-call? Because the proxy is made in step 7. A call to this.method() goes to the raw object, not the proxy.

Sources (2)
  1. Spring Framework reference: Lifecycle callbacks and combining mechanismsdocs.spring.io
  2. Spring Framework reference: Customizing beans with BeanPostProcessordocs.spring.io
Previous
Next