When you start a Spring Boot application, a lot happens behind the scenes before your code is ready to serve requests.
Just like beans have a lifecycle, the application itself goes through a well-defined sequence of events.
By understanding the Spring Application Lifecycle, you gain better control over initialization, startup, and graceful shutdown.
π The Spring Application Lifecycle (High-Level)
When you run a Spring Boot app (e.g., via SpringApplication.run()
), the following stages occur:
- Bootstrap Phase
-
SpringApplication
instance is created. - Application listeners and initializers are prepared.
- Environment Preparation
- Configuration properties (from
.properties
,.yml
, environment variables, command-line args, etc.) are loaded. - Profiles (e.g.,
dev
,prod
) are activated.
- ApplicationContext Creation
- The ApplicationContext (IoC container) is created.
- Bean definitions are loaded but not yet initialized.
- ApplicationContext Refresh
- Beans are instantiated, dependencies injected, and lifecycle methods triggered (the bean lifecycle we discussed earlier).
-
CommandLineRunner
andApplicationRunner
beans are executed.
- Application Ready
-
ApplicationReadyEvent
is published. - The app is now fully up and running.
- Shutdown Phase
-
On JVM shutdown (e.g.,
CTRL+C
, orSIGTERM
in containers), Spring performs cleanup:- Calls
@PreDestroy
/destroy()
methods on beans. - Closes the application context.
- Publishes
ContextClosedEvent
.
- Calls
π‘ Application Lifecycle Hooks
Spring provides multiple ways to hook into this lifecycle.
1. Application Events
Spring publishes events at different stages. You can listen to them:
@Component
public class MyAppEventsListener {
@EventListener(ApplicationStartingEvent.class)
public void onStart() {
System.out.println("π Application is starting...");
}
@EventListener(ApplicationReadyEvent.class)
public void onReady() {
System.out.println("β
Application is ready!");
}
@EventListener(ContextClosedEvent.class)
public void onShutdown() {
System.out.println("π Application is shutting down...");
}
}
2. CommandLineRunner and ApplicationRunner
Run code right after the application context is ready.
@Component
public class StartupRunner implements CommandLineRunner {
@Override
public void run(String... args) {
System.out.println("π Running startup logic...");
}
}
ApplicationRunner
is similar but gives you ApplicationArguments
instead of raw String[]
.
3. SmartLifecycle
For beans that need start/stop callbacks.
@Component
public class MyLifecycleBean implements SmartLifecycle {
private boolean running = false;
@Override
public void start() {
System.out.println("βΆοΈ Custom lifecycle bean started");
running = true;
}
@Override
public void stop() {
System.out.println("βΉ Custom lifecycle bean stopped");
running = false;
}
@Override
public boolean isRunning() {
return running;
}
}
4. DisposableBean / PreDestroy
During shutdown, Spring ensures resources are released:
@Component
public class ShutdownHook {
@PreDestroy
public void cleanup() {
System.out.println("π§Ή Cleaning up resources before shutdown...");
}
}
π Visualizing the Application Lifecycle
[SpringApplication Bootstrap]
β
[Environment Prepared]
β
[ApplicationContext Created]
β
[Beans Initialized & Context Refreshed]
β
[CommandLineRunner / ApplicationRunner Executed]
β
[ApplicationReadyEvent Published β App Running]
β
[ContextClosedEvent Published β Shutdown Hooks]
π Comparison of Lifecycle Hooks
Hook Type | When It Runs | Common Use Case |
---|---|---|
ApplicationStartingEvent |
At very start, before context created | Logging setup, metrics init |
ApplicationReadyEvent |
After startup is complete | Notify external systems, warm-up caches |
CommandLineRunner / ApplicationRunner
|
After context refresh, before ready | Startup logic, data seeding |
@PreDestroy / DisposableBean
|
Before shutdown | Resource cleanup |
SmartLifecycle |
Start/stop custom beans | Manage background tasks |
π Conclusion
The Spring Application Lifecycle ensures a smooth flow from startup β running β shutdown.
- Use Application Events to react to key stages.
- Use CommandLineRunner or ApplicationRunner for startup logic.
- Use @PreDestroy or SmartLifecycle for graceful shutdown.
By combining bean lifecycle hooks with the application lifecycle, you can build applications that are robust, predictable, and resource-safe.
Top comments (0)