Spring Boot Application Events and Listeners
An event is an excellent tool to decouple an action that is recognized by a consumer and triggered by a producer. Spring Boot comes with a set of out-of-the-box Application Events. These events provide different states and information about running applications. This feature allows the developer to react to any application state if necessary.
Listening to these events is pretty simple in Spring Boot and can be done by simply implementing the expected interface and adding it to the Spring Boot Application starter.
Getting Start
To start we need to create our Listener by implementing ApplicationListener<SpringApplicationEvent>
interface.
class SpringApplicationEventsListener
implements ApplicationListener<SpringApplicationEvent>{
@Override
public void onApplicationEvent(SpringApplicationEvent event) {
// event handling
System.out.println( " > " + event );
}
}
For the events that are triggered before the ApplicationContext
is created, we can use SpringApplication.addListeners(…)
or the SpringApplicationBuilder.listeners(…)
method to register your listener.
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(DemoApplication.class);
springApplication.addListeners(new SpringApplicationEventsListener());
springApplication.run(args);
}
}
Another option is that you can add a META-INF/spring.factories
file to your project and reference your listener(s) by using the org.springframework.context.ApplicationListener
key, if you want those listeners to be registered automatically regardless of how the application is created.
org.springframework.context.ApplicationListener=com.medi.listeners.SpringApplicationEventsListener
Now by running any of the above codes, you will notice that you can see multiple events being triggered at different stages of application startup.
For each received event you can cast the object to the expected object and get the proper information related to the event.
@Override
public void onApplicationEvent(SpringApplicationEvent event) {
if( event. instanceof ApplicationStartingEvent ){
((ApplicationStartingEvent) event).getSpringApplication();
}
}
To understand what dose each of these events means and when they triggered you can refer to the following list:
ApplicationStartingEvent
is triggered at the beginning of a run, but before any processing with the exception of listener and initializer registration.ApplicationEnvironmentPreparedEvent
is sent right before the context is created but the usingEnvironment
is known.ApplicationContextInitializedEvent
triggered before any bean definitions are loaded andApplicationContext
prepared.ApplicationPreparedEvent
is sent shortly after bean definitions have been loaded but before the refresh process begins.ApplicationStartedEvent
is triggered after the context has been updated but before the calling of any application and command-line runners.AvailabilityChangeEvent
is immediately sent withLivenessState.CORRECT
that stating that the application is live now.ApplicationReadyEvent
is triggered following the calling of any command line and application runners.AvailabilityChangeEvent
is received right after the application is ready to service requests.ApplicationFailedEvent
is sent in case there is an exception on startup.
In this article, we have discussed how you can consume the events published by the Spring but you can also create your own events and publish them so different components in your app using them. To do so you can read the following article:
I hope this article helped you, and please support me by applauding 👏 for the story. If you don’t know how it works, it’s just like this:
Or buy me a coffee here!