Application Events and Listeners
In addition to the usual Spring Framework events, such as ContextRefreshedEvent, a SpriingApplication sends some additional application events.
Application events are sent in the following order, as your application runs:
- An ApplicationStartingEvent is sent at the start of a run but before any processing, except for the registration of listeners and initializers.
- An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known but before the context is created.
- An ApplicationContextInitializedEvent is sent when the ApplicationContext is prepared and ApplicatoniContextInitializers have been called but before any bean definitions are loaded.
- An ApplicationPreparedEvent is sent just before the refresh is started but after bean definitions have been loaded.
- An ApplicationStartedEvent is sent after the context has been refreshed but before any application and command-line runners have been called.
- An AvailabilityChangeEvent is sent right after with LivenessState.CORRECT to indicate that the application is considered as live.
- An ApplicationReadyEvent is sent after any application and command-line runners have been called.
- An AvailabilityChangeEvent is sent right after with ReadinessState.ACCEPTING_TRAFFIC to indicate that the application is ready to service requsts.
- An ApplicationFailedEvent is sent if there is an exception on startup.
The above list onlu includes SpringApplicatoniEvents that are tied to a SpringApplication. In addition to these, the following events are also published after ApplicationPreparedEvent and before ApplicationStartedEvent.
- A WebServerInitializedEvent is sent after the WebSerber is ready. ServletWebServerInitilizedEvent and ReactiveWebServerInitializedEvent are the servlet and reactive variants respectively(各自的,分别的).
- A ContextRefreshedEvent is sent when an ApplicationContext is refreshed.
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.