Here is a clear, step-by-step lifecycle of a Spring Boot application, explained in a simple + interview-ready way.
π Spring Boot Application Lifecycle
1️⃣ Application Start (Bootstrap Phase)
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
What happens internally:
SpringApplication object created
Application type detected
Servlet (Tomcat)
Reactive (Netty)
Environment prepared
application.properties / ymlProfiles (
dev,test,prod)
Banner printed
ApplicationContext created
2️⃣ ApplicationContext Initialization
Spring creates the IoC container.
Key steps:
Classpath scanning
Load
@ConfigurationclassesProcess
@EnableAutoConfigurationApply
@ComponentScan
π Beans are defined, not created yet.
3️⃣ Bean Creation Phase (Core Lifecycle)
For each bean, Spring follows this order:
1. Instantiate Bean
2. Populate Properties (@Autowired)
3. Aware Interfaces
4. BeanPostProcessor (Before Init)
5. @PostConstruct / init-method
6. BeanPostProcessor (After Init)
Detailed Order:
πΉ 1. Bean Instantiation
Object is created using constructor
πΉ 2. Dependency Injection
@Autowired@Value
πΉ 3. Aware Interfaces (Optional)
BeanNameAware
ApplicationContextAware
EnvironmentAware
πΉ 4. BeanPostProcessor (Before Init)
postProcessBeforeInitialization()
πΉ 5. Initialization
@PostConstructInitializingBean.afterPropertiesSet()init-method
πΉ 6. BeanPostProcessor (After Init)
postProcessAfterInitialization()
π Bean is now ready to use
4️⃣ Embedded Server Startup
Embedded Tomcat / Jetty / Netty starts
DispatcherServlet initialized
Application is ready to serve requests
Log example:
Started MyApp in 4.5 seconds
5️⃣ Application Ready Events
Spring fires lifecycle events:
| Event | Purpose |
|---|---|
ApplicationStartingEvent | App starting |
ApplicationEnvironmentPreparedEvent | Env ready |
ApplicationContextInitializedEvent | Context created |
ApplicationPreparedEvent | Beans loaded |
ApplicationReadyEvent | App ready |
Example:
@EventListener(ApplicationReadyEvent.class)
public void onReady() {
System.out.println("App is ready");
}
6️⃣ Request Processing Lifecycle (Runtime)
Client Request
↓
Filter
↓
Interceptor (preHandle)
↓
Controller
↓
Service
↓
Repository
↓
Response
7️⃣ Graceful Shutdown (End of Lifecycle)
Triggered by:
Ctrl+C
SIGTERM
Kubernetes pod termination
Steps:
ContextClosedEvent@PreDestroyDisposableBean.destroy()destroy-methodEmbedded server stops
Example:
@PreDestroy
public void cleanup() {
System.out.println("App shutting down");
}
π§ Complete Lifecycle Diagram (Text)
main()
↓
SpringApplication.run()
↓
Prepare Environment
↓
Create ApplicationContext
↓
Bean Definition Load
↓
Bean Creation & Initialization
↓
Embedded Server Start
↓
ApplicationReadyEvent
↓
Application Running
↓
Shutdown Trigger
↓
Bean Destruction
↓
Context Closed
π― Interview One-Liner Answer
“Spring Boot lifecycle starts with
SpringApplication.run, prepares the environment, initializes the ApplicationContext, creates and initializes beans, starts the embedded server, handles requests, and finally performs graceful shutdown by destroying beans.”
⚠️ Common Interview Follow-Ups
Q. Difference between @PostConstruct and @Bean init-method?
π @PostConstruct is JSR-250, init-method is Spring-specific.
Q. When are BeanPostProcessors executed?
π Before and after bean initialization.
Q. When does embedded Tomcat start?
π After ApplicationContext is refreshed.
If you want:
π Lifecycle diagram image
π Lifecycle vs Spring Framework comparison
π Real production troubleshooting examples
π Bean lifecycle coding question
Just tell me π
No comments:
Post a Comment