Application Events and Listeners
In addition to the usual Spring Framework events, such as ContextRefreshedEvent, a SpringApplication sends some additional application events.
| Some events are actually triggered before the  If you want those listeners to be registered automatically, regardless of the way the application is created, you can add a  org.springframework.context.ApplicationListener=com.example.project.MyListener | 
Application events are sent in the following order, as your application runs:
- 
An ApplicationStartingEventis sent at the start of a run but before any processing, except for the registration of listeners and initializers.
- 
An ApplicationEnvironmentPreparedEventis sent when theEnvironmentto be used in the context is known but before the context is created.
- 
An ApplicationContextInitializedEventis sent when theApplicationContextis prepared and ApplicationContextInitializers have been called but before any bean definitions are loaded.
- 
An ApplicationPreparedEventis sent just before the refresh is started but after bean definitions have been loaded.
- 
An ApplicationStartedEventis sent after the context has been refreshed but before any application and command-line runners have been called.
- 
An AvailabilityChangeEventis sent right after withLivenessState.CORRECTto indicate that the application is considered as live.
- 
An ApplicationReadyEventis sent after any application and command-line runners have been called.
- 
An AvailabilityChangeEventis sent right after withReadinessState.ACCEPTING_TRAFFICto indicate that the application is ready to service requests.
- 
An ApplicationFailedEventis sent if there is an exception on startup.
The above list only includes SpringApplicationEvents that are tied to a SpringApplication.
In addition to these, the following events are also published after ApplicationPreparedEvent and before ApplicationStartedEvent:
- 
A WebServerInitializedEventis sent after theWebServeris ready.ServletWebServerInitializedEventandReactiveWebServerInitializedEventare the servlet and reactive variants respectively.
- 
A ContextRefreshedEventis sent when anApplicationContextis refreshed.
| You often need not use application events, but it can be handy to know that they exist. Internally, Spring Boot uses events to handle a variety of tasks. | 
| Event listeners should not run potentially lengthy tasks as they execute in the same thread by default. Consider using application and command-line runners instead. | 
Application events are sent by using Spring Framework’s event publishing mechanism.
Part of this mechanism ensures that an event published to the listeners in a child context is also published to the listeners in any ancestor contexts.
As a result of this, if your application uses a hierarchy of SpringApplication instances, a listener may receive multiple instances of the same type of application event.
To allow your listener to distinguish between an event for its context and an event for a descendant context, it should request that its application context is injected and then compare the injected context with the context of the event.
The context can be injected by implementing ApplicationContextAware or, if the listener is a bean, by using @Autowired.