Java Interview Questions

Spring Bean Lifecycle: From Instantiation to Destruction

"Walk me through the Spring Bean lifecycle."

Why Interviewers Ask This

The Spring lifecycle is a canonical Java backend interview question. Interviewers use it to test whether you understand the framework's extension points (BeanPostProcessor, Aware interfaces) well enough to extend it — not just use @Autowired.

How to Answer

  1. 1

    Instantiation: Spring calls the bean's constructor (or @Configuration factory method) to create the raw object.

  2. 2

    Property population: Spring resolves dependencies and injects properties via setter, field, or constructor injection. @Autowired is processed here.

  3. 3

    Aware callbacks: if the bean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, Spring calls the corresponding setXxx method to inject framework context.

  4. 4

    BeanPostProcessor.postProcessBeforeInitialization runs on every bean; then initialization runs — @PostConstruct, then InitializingBean.afterPropertiesSet, then the bean's custom init-method if configured.

  5. 5

    BeanPostProcessor.postProcessAfterInitialization runs — this is where AOP proxies are created by AnnotationAwareAspectJAutoProxyCreator. The bean is now ready for use.

  6. 6

    Destruction on shutdown: @PreDestroy, then DisposableBean.destroy(), then custom destroy-method.

Sample Answer

The Spring bean lifecycle has six main phases. First, instantiation — Spring calls the constructor or the @Bean factory method to create the raw object. Second, property population — Spring resolves and injects dependencies; @Autowired, @Value, @Resource are all handled here by the AutowiredAnnotationBeanPostProcessor. Third, Aware callbacks — if the bean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, EnvironmentAware, Spring calls the respective setter to hand the bean a reference to framework internals it needs. Fourth, BeanPostProcessor.postProcessBeforeInitialization runs across every bean — this is the hook that lets frameworks intercept. Then initialization: @PostConstruct first, then InitializingBean.afterPropertiesSet(), then a custom init-method if one is declared on the BeanDefinition. Fifth, BeanPostProcessor.postProcessAfterInitialization runs — crucially this is where AOP proxies get wrapped around the bean. AnnotationAwareAspectJAutoProxyCreator is itself a BeanPostProcessor, and when it sees a bean matching a pointcut, it wraps the original bean in a CGLIB or JDK dynamic proxy and returns the proxy to the container. From this point on, the ApplicationContext and anything that @Autowires this bean gets the proxy, not the original. Sixth, destruction on context shutdown: @PreDestroy first, then DisposableBean.destroy(), then a custom destroy-method. A key corollary: AOP being in post-init is why calling one @Transactional method from another method of the same class bypasses the transaction — the inner call is on "this" directly, not on the proxy. The fix is to get the proxy via AopContext.currentProxy() or inject the bean into itself.

Pro Tips

  • Always tie the AOP step to the "self-invocation bypasses transactions" gotcha — a frequent follow-up question.

  • Know the three init-methods in order: @PostConstruct → InitializingBean → init-method.

  • Being able to name specific BeanPostProcessors like AnnotationAwareAspectJAutoProxyCreator signals deep familiarity.

  • If you freeze on the order of init callbacks, HireMe AI can surface the exact sequence in real time.

Frequently Asked Questions

When are circular dependencies resolved?

Spring handles setter/field injection cycles using a three-level cache — during "populate properties", it can expose an early reference of a still-initializing bean from the second level cache to break the cycle. Constructor cycles can't be resolved and fail.

What's the difference between BeanFactory and ApplicationContext?

BeanFactory is the minimal IoC container. ApplicationContext adds eager initialization, events, i18n, resource loading — a superset. Most applications use ApplicationContext.

Can a bean be prototype scoped in the lifecycle?

Yes, but Spring only calls init methods for prototypes — destruction is the caller's responsibility, since the container doesn't track prototypes after they leave.

Worried About Going Blank in Your Interview?

HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.

Try HireMe AI Free