Quick revision
The short answer
Spring creates a singleton in a fixed order: it instantiates the object (constructor injection happens here), populates field and setter injection, calls the Aware interfaces, runs postProcessBeforeInitialization on every BeanPostProcessor (which is where @PostConstruct runs), then afterPropertiesSet and any custom init method, and finally postProcessAfterInitialization, where AOP proxies are usually created. At shutdown it calls @PreDestroy, then DisposableBean.destroy(), then the custom destroy method.
What to remember
In this answer, we followed a singleton through its lifecycle: instantiation with constructor injection, population of fields and setters, the Aware callbacks, postProcessBeforeInitialization where @PostConstruct runs, afterPropertiesSet and the custom init method, and postProcessAfterInitialization where the AOP proxy usually replaces the bean. We saw the hooks that run after all singletons exist, and the shutdown order: SmartLifecycle stop, then @PreDestroy, DisposableBean.destroy and the custom or inferred destroy method for each bean.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring creates a singleton in a fixed order: it instantiates the object (constructor injection happens here), populates field and setter injection, calls the Aware interfaces, runs postProcessBeforeInitialization on every BeanPostProcessor (which is where @PostConstruct runs), then afterPropertiesSet and any custom init method, and finally postProcessAfterInitialization, where AOP proxies are usually created. At shutdown it calls @PreDestroy, then DisposableBean.destroy(), then the custom destroy method.
Why this question is asked
The bean lifecycle sounds like theory, but almost every confusing Spring bug sits on one of its steps: a field that is still null in the constructor, a @Transactional method that does nothing inside @PostConstruct, a resource that is never closed. Interviewers ask for the steps because knowing them lets us predict where such bugs come from. In this answer, we will build one bean that prints a line at every step, walk through creation from instantiation to the moment the bean is ready, see where the BeanPostProcessors and proxies fit, and then follow the shutdown steps.
1A Quick Recap: Who Creates Beans and When
An application context reads bean definitions, which are descriptions of the beans to create: their class, scope, dependencies and callbacks. At the end of its refresh it creates every non-lazy singleton, one bean at a time, using a method in AbstractAutowireCapableBeanFactory called doCreateBean. Each singleton passes through the same steps, and when all have passed, the context is ready.
Two terms appear throughout. A callback is a method of our bean that Spring calls at a certain step, such as a @PostConstruct method. A BeanPostProcessor is a special bean that Spring calls for every other bean before and after its initialisation, so it can inspect, change or replace it. Much of Spring's own behaviour, including injection of @Autowired fields and creation of proxies, is built from post-processors.
2Watching Every Step With One Bean
The class below implements every callback and prints a line in each, so we can see the order for ourselves.
3Creation, Step by Step
Step one is instantiation. Spring picks a constructor, or calls a factory method for a @Bean, and resolves its arguments first. This is where constructor injection happens, so every constructor-injected dependency is already a finished bean. The object now exists, but its @Autowired fields are still null.
Step two is population. AutowiredAnnotationBeanPostProcessor, a post-processor that runs at this moment through its postProcessProperties hook, fills @Autowired and @Value fields and calls @Autowired setters. The reason field injection happens after the constructor is simple: Java can only set fields on an object that already exists. That is why reading an @Autowired field in the constructor gives null.
Step three is the Aware callbacks. If the bean implements BeanNameAware, BeanClassLoaderAware or BeanFactoryAware, Spring passes it its name, class loader or factory. ApplicationContextAware and the other context-level Aware interfaces are handled a moment later by a post-processor, but from our point of view they belong to the same step: the bean learns about its container.
Step four is postProcessBeforeInitialization on every BeanPostProcessor. One of them, CommonAnnotationBeanPostProcessor, finds the @PostConstruct method and calls it here. Then step five, afterPropertiesSet() if the bean implements InitializingBean, and step six, the custom init method named in @Bean(initMethod = ...). Steps four to six all run after injection, so the bean can safely use its dependencies.
Step seven is postProcessAfterInitialization on every BeanPostProcessor. This is where the auto-proxy creator checks whether the bean needs a proxy for @Transactional, @Cacheable or an aspect, and if so returns a proxy that wraps it; AsyncAnnotationBeanPostProcessor does the same for @Async. Notice the consequence: whatever this step returns is what the context stores and what other beans get. During steps one to six our code was running on the raw object, without any proxy.
- Instantiateconstructor or factory method, constructor injection
- Populate properties
@Autowiredfields and setters,@Value - Aware callbacks
BeanNameAware,BeanFactoryAware,ApplicationContextAware - BPP before initialization
@PostConstructruns here - afterPropertiesSet, then custom init method
InitializingBean,@Bean(initMethod) - BPP after initializationAOP proxy usually created here
- Bean is readystored in the singleton cache, injected into others
7 steps in order. Play them, or step through them one at a time.
4After Every Singleton Exists
Some work should only happen once all beans exist, not inside one bean's creation. Spring offers two hooks for it. After every non-lazy singleton is created, Spring calls afterSingletonsInstantiated() on each bean that implements SmartInitializingSingleton. Then the context starts every Lifecycle and SmartLifecycle bean, such as an embedded web server or a message listener container, and publishes ContextRefreshedEvent. Spring Boot adds ApplicationStartedEvent and ApplicationReadyEvent after that.
These later hooks matter because the steps above run per bean. A @PostConstruct method can rely on its own dependencies, but not on beans it does not reference, which may not exist yet.
5The Shutdown Steps
When the context closes, for example on SIGTERM in a container or when a test context is discarded, it first stops all SmartLifecycle beans in phase order, which is how a web server stops taking requests before anything else shuts down. Then it destroys the singletons, in roughly the reverse order of their creation, so a bean is destroyed before the beans it depends on.
For each bean, Spring calls @PreDestroy first (again through CommonAnnotationBeanPostProcessor, acting as a destruction-aware post-processor), then destroy() if it implements DisposableBean, then the custom destroy method. For @Bean methods, Spring Boot and Spring infer a public close() or shutdown() method as the destroy method by default, which is why a connection pool defined with @Bean gets closed without any configuration.
| Order | What runs | Example |
|---|---|---|
| 1 | SmartLifecycle.stop by phase | web server stops accepting, drains requests |
| 2 | @PreDestroy | flush a buffer |
| 3 | DisposableBean.destroy | framework beans release resources |
| 4 | custom or inferred destroy method | HikariDataSource.close() |
6Follow-up questions
Are these steps the same for prototype beans?
Creation is the same: instantiate, populate, Aware, the post-processors and init callbacks all run. But Spring does not keep track of prototype instances after handing them out, so none of the destruction callbacks are called. Whoever asked for the prototype is responsible for cleaning it up.
Where exactly is the constructor-injection dependency created?
Before step one of our bean. To call our constructor, Spring resolves each argument by getting or creating that bean, which runs that bean's full lifecycle first. So a constructor-injected dependency is always finished, unless it is a lazy proxy or part of a cycle.
Why does a @Transactional method called from @PostConstruct not start a transaction?
Because @PostConstruct runs in step four, on the raw object, and the proxy that adds transactions is only created in step seven. Even after that, a call on this bypasses the proxy. Running the work on ApplicationReadyEvent through the proxy, or in another bean, fixes it.
7Conclusion
In this answer, we followed a singleton through its lifecycle: instantiation with constructor injection, population of fields and setters, the Aware callbacks, postProcessBeforeInitialization where @PostConstruct runs, afterPropertiesSet and the custom init method, and postProcessAfterInitialization where the AOP proxy usually replaces the bean. We saw the hooks that run after all singletons exist, and the shutdown order: SmartLifecycle stop, then @PreDestroy, DisposableBean.destroy and the custom or inferred destroy method for each bean.
Open with
For a singleton, Spring first instantiates it, calling the constructor or factory method, and that is where constructor injection happens, so those dependencies are already finished beans. Then it populates properties: AutowiredAnnotationBeanPostProcessor fills @Autowired fields and setters and @Value, which is why a field is still null inside the constructor.
Then, if they want more
Next come the Aware callbacks, like BeanNameAware, BeanFactoryAware and ApplicationContextAware. Then every BeanPostProcessor gets postProcessBeforeInitialization, and that is where CommonAnnotationBeanPostProcessor calls the @PostConstruct method. After that Spring calls afterPropertiesSet if the bean implements InitializingBean, and then a custom init method if one is declared on @Bean.
Finally every post-processor gets postProcessAfterInitialization, which is where the auto-proxy creator usually wraps the bean in a proxy for @Transactional or aspects, and whatever it returns is the bean everyone else sees. Once all singletons exist, SmartInitializingSingleton callbacks run and lifecycle beans like the web server start.
At shutdown, SmartLifecycle beans stop first, then for each singleton Spring calls @PreDestroy, then DisposableBean.destroy, then the custom or inferred destroy method, like close on a pool. Prototypes get no destroy callbacks.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "List the steps Spring goes through to create a singleton bean, from instantiation to...": https://scrivio-interview-library.netlify.app/spring-boot/bean-lifecycle/05861faf8e/
Spring Boot backend interviews, question 3 of 16 Back to the path
More in Bean lifecycle