0
0
Spring Bootframework~10 mins

Bean lifecycle overview in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Bean lifecycle overview
Bean Definition Loaded
Bean Instantiation
Populate Properties
BeanNameAware.setBeanName()
BeanFactoryAware.setBeanFactory()
Pre-initialization PostProcessors
InitializingBean.afterPropertiesSet()
Custom init-method
Bean Ready to Use
Container Shutdown
DisposableBean.destroy()
Custom destroy-method
Bean Destroyed
This flow shows the main steps a Spring Bean goes through from creation to destruction.
Execution Sample
Spring Boot
public class MyBean implements InitializingBean, DisposableBean {
  @Override
  public void afterPropertiesSet() { System.out.println("Init"); }
  @Override
  public void destroy() { System.out.println("Destroy"); }
}
A simple bean that prints messages during initialization and destruction phases.
Execution Table
StepActionMethod CalledState/Output
1Bean definition loaded by SpringN/ABean metadata ready
2Spring creates bean instanceConstructorBean object created
3Spring sets bean propertiesSetter methodsProperties set
4Spring calls setBeanName()BeanNameAware.setBeanName()Bean name injected
5Spring calls setBeanFactory()BeanFactoryAware.setBeanFactory()Bean factory injected
6Spring applies pre-initialization processorsBeanPostProcessor.postProcessBeforeInitialization()Bean processed
7Spring calls afterPropertiesSet()InitializingBean.afterPropertiesSet()Custom init logic runs
8Spring calls custom init-methodCustom init methodAdditional init logic runs
9Bean is ready for useN/ABean fully initialized
10Container shutdown beginsN/AShutdown triggered
11Spring calls destroy()DisposableBean.destroy()Cleanup logic runs
12Spring calls custom destroy-methodCustom destroy methodAdditional cleanup runs
13Bean destroyed and removedN/ABean lifecycle ends
💡 Bean lifecycle ends after destruction methods complete during container shutdown.
Variable Tracker
VariableStartAfter InstantiationAfter Properties SetAfter InitializationAfter Destruction
Bean InstancenullCreatedProperties SetInitializedDestroyed
Bean NamenullInjectedInjectedInjectedInjected
Bean FactorynullInjectedInjectedInjectedInjected
Key Moments - 3 Insights
Why does Spring call setBeanName() before initialization?
Spring calls setBeanName() early so the bean knows its own name before any initialization logic runs, as shown in step 4 of the execution_table.
What is the difference between afterPropertiesSet() and a custom init-method?
afterPropertiesSet() is from the InitializingBean interface and runs automatically, while a custom init-method is a user-defined method configured in the bean definition; both run during initialization (steps 7 and 8).
When does the bean get destroyed?
The bean is destroyed during container shutdown, after the destroy() method and custom destroy-method run, as shown in steps 11-13.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does Spring inject the bean's name?
AStep 2
BStep 4
CStep 7
DStep 10
💡 Hint
Check the 'Method Called' column for setBeanName() in the execution_table.
According to variable_tracker, what is the state of the Bean Instance after properties are set?
AProperties Set
BCreated
CDestroyed
DInitialized
💡 Hint
Look at the 'After Properties Set' column for 'Bean Instance' in variable_tracker.
If the custom init-method is removed, which step in the execution_table would be skipped?
AStep 7
BStep 11
CStep 8
DStep 4
💡 Hint
Step 8 shows the custom init-method call in the execution_table.
Concept Snapshot
Bean lifecycle in Spring Boot:
1. Bean is instantiated and properties set.
2. Awareness interfaces inject context (name, factory).
3. Initialization callbacks run (afterPropertiesSet, custom init).
4. Bean is ready for use.
5. On shutdown, destroy callbacks run (destroy, custom destroy).
This ensures beans are properly prepared and cleaned up.
Full Transcript
The Spring Boot bean lifecycle starts when the container loads the bean definition. Spring creates the bean instance and sets its properties. Then, it calls awareness methods like setBeanName and setBeanFactory to inject context. Before the bean is ready, Spring runs initialization callbacks such as afterPropertiesSet and any custom init-method. After initialization, the bean is ready for use. When the container shuts down, Spring calls destroy methods to clean up resources. This lifecycle ensures beans are properly created, initialized, and destroyed in an orderly way.