Kubernate

Thursday, December 18, 2025

Spring Boot - Bean LifeCycle

 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:

  1. SpringApplication object created

  2. Application type detected

    • Servlet (Tomcat)

    • Reactive (Netty)

  3. Environment prepared

    • application.properties / yml

    • Profiles (dev, test, prod)

  4. Banner printed

  5. ApplicationContext created


2️⃣ ApplicationContext Initialization

Spring creates the IoC container.

Key steps:

  • Classpath scanning

  • Load @Configuration classes

  • Process @EnableAutoConfiguration

  • Apply @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

  • @PostConstruct

  • InitializingBean.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:

EventPurpose
ApplicationStartingEventApp starting
ApplicationEnvironmentPreparedEventEnv ready
ApplicationContextInitializedEventContext created
ApplicationPreparedEventBeans loaded
ApplicationReadyEventApp 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:

  1. ContextClosedEvent

  2. @PreDestroy

  3. DisposableBean.destroy()

  4. destroy-method

  5. Embedded 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

Spring Boot - Bean LifeCycle

 Here is a clear, step-by-step lifecycle of a Spring Boot application , explained in a simple + interview-ready way. πŸ”„ Spring Boot Applica...

Kubernate