23.5 Application events and listeners
In addition to the usual Spring Framework events, such as ContextRefreshedEvent
, a SpringApplication
sends some additional application events.
Note | |
---|---|
Some events are actually triggered before the ApplicationContext is created so you cannot register a listener on those as a @Bean . You can register them via the SpringApplication.addListeners(…) or SpringApplicationBuilder.listeners(…) methods. |
Application events are sent in the following order, as your application runs:
- An
ApplicationStartedEvent
is sent at the start of a run, but before any processing except the registration of listeners and initializers. - An
ApplicationEnvironmentPreparedEvent
is sent when theEnvironment
to be used in the context is known, but before the context is created. - An
ApplicationPreparedEvent
is sent just before the refresh is started, but after bean definitions have been loaded. - An
ApplicationReadyEvent
is sent after the refresh and any related callbacks have been processed to indicate the application is ready to service requests. - An
ApplicationFailedEvent
is sent if there is an exception on startup.
Tip | |
---|---|
You often won’t need to use application events, but it can be handy to know that they exist. Internally, Spring Boot uses events to handle a variety of tasks. |