What is the Spring bean lifecycle and what callbacks does it provide?
Understand the Spring bean lifecycle from instantiation to destruction and the callbacks it provides — @PostConstruct, @PreDestroy, and more.
Expected Interview Answer
The Spring bean lifecycle is the sequence of steps the container runs from instantiating a bean to destroying it, including dependency injection, initialization callbacks, active use, and destruction callbacks. Spring exposes hooks such as @PostConstruct, @PreDestroy, InitializingBean, DisposableBean, and custom init/destroy methods.
After instantiation and property population, Spring invokes Aware interfaces, then BeanPostProcessor before-init logic, then initialization callbacks (@PostConstruct, afterPropertiesSet, custom init-method), then BeanPostProcessor after-init logic. The bean is then ready for use. On container shutdown, destruction callbacks (@PreDestroy, destroy, custom destroy-method) run — but only for singletons, since Spring does not manage prototype destruction. The preferred, framework-agnostic hooks are the JSR-250 annotations @PostConstruct and @PreDestroy.
- Lets you run setup logic once dependencies are injected
- Gives a clean place to release resources like connections and files
- BeanPostProcessors enable cross-cutting customization (e.g. proxies)
- Aware interfaces expose container infrastructure when needed
- Standard annotations keep code decoupled from Spring interfaces
AI Mentor Explanation
A bean's lifecycle is like a cricketer's match day: selection and kitting up is instantiation and dependency injection, the pre-match warm-up and net session is the initialization callback that runs before play, the innings itself is the active in-use phase, and the cool-down and equipment stowing after stumps is the destroy callback that releases everything cleanly.
Step-by-Step Explanation
Step 1
Instantiation
The container constructs the bean instance by calling its constructor.
Step 2
Populate properties
Spring injects dependencies via constructor, setter, or field injection.
Step 3
Aware and post-process before init
Aware interfaces run, then BeanPostProcessor.postProcessBeforeInitialization executes.
Step 4
Initialization callbacks
@PostConstruct runs, then InitializingBean.afterPropertiesSet, then any custom init-method.
Step 5
Post-process after init and use
postProcessAfterInitialization runs (often creating proxies), then the bean is ready for use.
Step 6
Destruction
On shutdown Spring calls @PreDestroy, DisposableBean.destroy, then the custom destroy-method for singletons.
What Interviewer Expects
- Ordered understanding of instantiate, inject, init, use, destroy
- Naming @PostConstruct and @PreDestroy as preferred hooks
- Knowing InitializingBean and DisposableBean interfaces
- Role of BeanPostProcessor in the lifecycle
- Awareness that prototype destroy callbacks are not invoked
Common Mistakes
- Confusing BeanPostProcessor with BeanFactoryPostProcessor
- Expecting @PreDestroy to run on prototype beans
- Putting initialization logic in the constructor before injection completes
- Thinking destroy callbacks fire on JVM kill -9 or abrupt crashes
- Coupling code to Spring interfaces instead of using JSR-250 annotations
Best Answer (HR Friendly)
“The bean lifecycle is just the journey an object takes inside Spring — it gets created, its dependencies are filled in, it runs some setup, it is used, and finally it is cleaned up. Spring gives you hook methods to plug into the setup and cleanup steps.”
Code Example
@Component
public class ConnectionManager {
private Connection connection;
@PostConstruct
public void init() {
// runs after dependencies are injected
this.connection = openConnection();
}
@PreDestroy
public void cleanup() {
// runs on container shutdown (singletons only)
if (connection != null) connection.close();
}
}@Component
public class CacheService implements InitializingBean, DisposableBean {
@Override
public void afterPropertiesSet() {
warmUpCache();
}
@Override
public void destroy() {
flushCache();
}
}Follow-up Questions
- What is the difference between BeanPostProcessor and BeanFactoryPostProcessor?
- In what order do @PostConstruct, afterPropertiesSet, and init-method run?
- Why are destroy callbacks skipped for prototype beans?
- How does Spring create proxies during the bean lifecycle?
- What do the Aware interfaces provide to a bean?
MCQ Practice
1. Which annotation marks a method to run right after dependency injection?
@PostConstruct marks an initialization method that Spring invokes once the bean is constructed and its dependencies injected.
2. For which beans does Spring NOT invoke destroy callbacks?
Spring hands prototype instances to the caller and does not track them, so it never calls their destroy callbacks.
3. Which interface provides the afterPropertiesSet() initialization hook?
InitializingBean declares afterPropertiesSet(), called after properties are populated.
Flash Cards
Preferred init/destroy hooks? — JSR-250 annotations @PostConstruct and @PreDestroy.
Interface for init callback? — InitializingBean with afterPropertiesSet().
Interface for destroy callback? — DisposableBean with destroy().
Does prototype get destroy callbacks? — No — Spring does not manage prototype destruction.
What runs around initialization for all beans? — BeanPostProcessor before- and after-initialization methods.
Continue Learning
Related Interview Questions
What are Spring bean scopes and how do singleton and prototype differ?
medium
What is the difference between @Autowired field, constructor, and setter injection?
medium
What is @SpringBootApplication and what annotations does it combine?
easy
How does Spring Boot handle application configuration with application.properties and profiles?
medium