Module: Spring Boot Fundamentals

Application Lifecycle

Spring Boot Fundamentals: Application Lifecycle

This section dives into the lifecycle of a Spring Boot application, from startup to shutdown. Understanding this lifecycle is crucial for debugging, monitoring, and optimizing your applications.

1. Startup Phase

The Spring Boot application lifecycle begins when you execute the packaged JAR or run the application from your IDE. This phase can be broken down into several key steps:

  • Bootstrap: The org.springframework.boot.loader.JarLauncher (or WarLauncher for WAR deployments) is the entry point. It's responsible for setting up the classloader and loading the Spring Boot application. This is different from a traditional servlet container startup. Spring Boot embeds its own container (Tomcat, Jetty, or Undertow).
  • Context Creation: Spring's ApplicationContext is created. This is the core container that manages beans, dependencies, and application configuration. Spring Boot automatically configures the ApplicationContext based on your dependencies, properties, and annotations.
  • Bean Definition & Instantiation: Spring scans your application for components (beans) annotated with @Component, @Service, @Repository, @Controller, etc. It then defines these beans and instantiates them. Dependency Injection (DI) happens during this stage, resolving dependencies between beans.
  • Application Listener Invocation: ApplicationListeners (implementing ApplicationListener<ApplicationEvent>) are notified about various events during the startup process. Common events include ApplicationStartingEvent, ApplicationEnvironmentPreparedEvent, ApplicationPreparedEvent, and ApplicationStartedEvent. These listeners allow you to perform custom actions at specific points in the startup sequence.
  • Command Line Runner/Runner Execution: If you've implemented CommandLineRunner or ApplicationRunner interfaces, their run() methods are executed after the ApplicationContext is fully loaded. This is a good place to perform initialization tasks that require access to the fully configured application context.
  • Server Startup (Embedded Container): The embedded web server (Tomcat, Jetty, or Undertow) is started. This involves initializing the server, deploying your application's web components (controllers, views, etc.), and listening for incoming requests.
  • Application Ready Event: Once the server is fully started and ready to handle requests, the ApplicationReadyEvent is fired. This signals that the application is fully operational.

2. Running Phase

Once the application is started, it enters the running phase. This is where it handles incoming requests, processes data, and performs its core functionality.

  • Request Handling: The embedded web server receives HTTP requests and routes them to the appropriate controllers.
  • Business Logic Execution: Controllers invoke services, which in turn interact with repositories to access data.
  • Data Persistence: Repositories interact with databases or other data stores to read and write data.
  • Event Publication: Components can publish events using the ApplicationEventPublisher. Other components can subscribe to these events and react accordingly.
  • Monitoring & Logging: Spring Boot provides built-in support for monitoring and logging, allowing you to track application performance and diagnose issues. Actuator provides endpoints for health checks, metrics, and other operational information.

3. Shutdown Phase

When you stop the application (e.g., by pressing Ctrl+C in the terminal or stopping the server in your IDE), the shutdown phase begins.

  • Graceful Shutdown: Spring Boot attempts to shut down the application gracefully. This involves:
    • Stopping the Web Server: The embedded web server stops accepting new connections and finishes processing existing requests. The server.shutdown property (default 60 seconds) controls the timeout for this process.
    • Destroying Beans: Beans that implement the DisposableBean interface have their destroy() method called. Alternatively, beans can be destroyed using the @PreDestroy annotation.
    • Application Listener Invocation: ApplicationListeners are notified about the shutdown process via the ApplicationStoppingEvent and ApplicationStoppedEvent.
  • Resource Release: Resources such as database connections, file handles, and network sockets are released.
  • Context Closure: The ApplicationContext is closed, releasing all managed beans and resources.

Key Events & Listeners

Event Description When it's fired
ApplicationStartingEvent Fired before any initialization begins. Very early in the startup process
ApplicationEnvironmentPreparedEvent Fired after the environment has been prepared but before the context is created. After properties are loaded, before context
ApplicationPreparedEvent Fired after the ApplicationContext is created but before it's refreshed. After context creation, before bean instantiation
ApplicationStartedEvent Fired after the ApplicationContext has been refreshed and the embedded server has started. After context refresh and server start
ApplicationReadyEvent Fired after the application is fully started and ready to handle requests. After server is ready to accept requests
ApplicationStoppingEvent Fired before the application shuts down. Before shutdown begins
ApplicationStoppedEvent Fired after the application has shut down. After shutdown is complete

Customizing the Lifecycle

You can customize the application lifecycle by:

  • Implementing ApplicationListener: Listen for specific events and perform custom actions.
  • Implementing CommandLineRunner or ApplicationRunner: Execute code after the ApplicationContext is fully loaded.
  • Using @PreDestroy: Define a method to be called before a bean is destroyed.
  • Implementing DisposableBean: Implement the destroy() method to perform cleanup tasks.
  • Configuring server.shutdown: Adjust the graceful shutdown timeout.

Understanding the Spring Boot application lifecycle empowers you to build more robust, manageable, and efficient applications. It provides a framework for handling initialization, runtime operations, and shutdown procedures in a controlled and predictable manner.