Spring boot provides an easy and quick way to implement the event listeners. Spring boot generates multiple events during it's application lifecycle. We will see the below events which are provided by spring application under "org.springframework.boot.context.event" Java package. Thanks to spring that we don't need to focus on writing the listeners but the business logics which we want to execute during a specific event and to do that we just need to annotate our business method with @EventListener.
- ApplicationContextInitializedEvent
Event published when a SpringApplication is starting up and the ApplicationContext is prepared and ApplicationContextInitializers have been called but before any bean definitions are loaded.
Implementation and code example
- ApplicationEnvironmentPreparedEvent
Event published when a SpringApplication is starting up and the Environment is first available for inspection and modification.
Implementation and code example
- ApplicationFailedEvent
Event published by a SpringApplication when it fails to start.
Implementation and code example
- ApplicationPreparedEvent
Event published as when a SpringApplication is starting up and the ApplicationContext is fully prepared but not refreshed. The bean definitions will be loaded and the Environment is ready for use at this stage.
For implementation you can refer any of the other events defined here, for example:- ApplicationReadyEvent or ApplicationStartedEvent in below sections.
For implementation you can refer any of the other events defined here, for example:- ApplicationReadyEvent or ApplicationStartedEvent in below sections.
- ApplicationReadyEvent
Event published as late as conceivably possible to indicate that the application is ready to service requests. The source of the event is the SpringApplication itself, but beware of modifying its internal state since all initialization steps will have been completed by then.
Implementation and code example
- ApplicationStartedEvent
Event published once the application context has been refreshed but before any application and command line runners have been called.
Implementation and code example
- ApplicationStartingEvent
Event published as early as conceivably possible as soon as a SpringApplication has been started - before the Environment or ApplicationContext is available, but after the ApplicationListeners have been registered. The source of the event is the SpringApplication itself, but beware of using its internal state too much at this early stage since it might be modified later in the lifecycle.
Comments
Post a Comment