Java Zero to All All questionsPractise free
Interview questions › Spring & Spring Boot › IoC, DI & Bean Lifecycle

Explain the Spring bean lifecycle from instantiation to destruction, and where AOP proxies appear.

Level III · MidIoC, DI & Bean Lifecycle
Answer

Spring instantiates the bean (constructor), populates properties and @Autowired dependencies, fires *Aware callbacks, then BeanPostProcessor.postProcessBeforeInitialization, then init callbacks (@PostConstruct, then InitializingBean.afterPropertiesSet, then a custom init-method), then BeanPostProcessor.postProcessAfterInitialization. AOP proxies for @Transactional, @Async, and @Cacheable are created in that final after-initialization step, which is exactly why a self-call bypasses them. On shutdown the container runs @PreDestroy, DisposableBean.destroy, then any destroy-method, in reverse creation order.

Common mistakes
  • Forgetting where BeanPostProcessors and AOP proxies are created.
  • Saying @PreDestroy runs for prototype beans.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for IoC, DI & Bean Lifecycle.

Practise free

More on IoC, DI & Bean Lifecycle