Medium java spring

Walk through Spring bean lifecycle internals, custom init and post-processing.

The 30 second answer

Spring creates the instance, injects dependencies, and calls Aware callbacks. Then bean post-processors run their before-initialization step, followed by init callbacks in order: PostConstruct, afterPropertiesSet, then a custom init method. After that the after-initialization step runs, and that is where AOP proxies are created. On shutdown it calls PreDestroy, then destroy, then a custom destroy method.

Spring instantiates the bean, injects dependencies, calls Aware callbacks, then BeanPostProcessor.postProcessBeforeInitialization, then init callbacks in this order: @PostConstruct, InitializingBean.afterPropertiesSet(), a custom init-method. Then postProcessAfterInitialization, which is where AOP proxies are created. On shutdown, @PreDestroy, DisposableBean.destroy(), then a custom destroy method. Follow-up: Why can't a BeanPostProcessor bean get proxied by AOP? (It is created early, before other post-processors are ready; the docs warn about this.)

Sources (2)
  1. Spring: Customizing the nature of a bean
  2. Spring: Container extension points