Default Events

Spring use events to handle variety of tasks. These events are Spring Frameworks internal events that will be handle in the same thread.

For the list of default event, visit: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.spring-application.application-events-and-listeners

Example listening to event

We can listen to event by implement the ApplicationListener<E>

For example:

@Component  
public class MyEventListener implements ApplicationListener<AvailabilityChangeEvent<?>> {  
  private static final Logger log = LoggerFactory.getLogger(MyEventListener.class);  
  
  @Override  
  public void onApplicationEvent(AvailabilityChangeEvent event) {  
    log.info(event.getState().toString());  
  }  
}

As a result, this prints out:

2024-03-11T20:36:41.953+11:00  INFO 354942 --- [  restartedMain] dev.auspham.listeners.MyEventListener    : CORRECT
2024-03-11T20:36:41.954+11:00  INFO 354942 --- [  restartedMain] dev.auspham.listeners.MyEventListener    : ACCEPTING_TRAFFIC