Spring Boot - Listening to Application Events with @EventListener

[Updated: Sep 7, 2017, Created: Sep 7, 2017]

In this quick example, we will use Spring core annotation @EventListener (tutorial here) to see what application events are fired during start up.

Example

@Component
public class ListenerBean {
  @EventListener
  public void handleEvent(Object event) {
      System.out.println("event: "+event);
  }
}

The Main class

@SpringBootApplication
public class ExampleMain {

  public static void main(String[] args) throws InterruptedException {
      SpringApplication sa = new SpringApplication(ExampleMain.class);
      ConfigurableApplicationContext context = sa.run(args);
      MyBean bean = context.getBean(MyBean.class);
      bean.doSomething();
  }
}

Output

2017-09-07 11:38:38.988  INFO 8212 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6d3af739: startup date [Thu Sep 07 11:38:38 CDT 2017]; root of context hierarchy
2017-09-07 11:38:39.370 INFO 8212 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
event: org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@6d3af739: startup date [Thu Sep 07 11:38:38 CDT 2017]; root of context hierarchy]
event: org.springframework.boot.context.event.ApplicationReadyEvent[source=org.springframework.boot.SpringApplication@14fc5f04]
-- in doSomething() --
2017-09-07 11:38:39.379 INFO 8212 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6d3af739: startup date [Thu Sep 07 11:38:38 CDT 2017]; root of context hierarchy
event: org.springframework.context.event.ContextClosedEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@6d3af739: startup date [Thu Sep 07 11:38:38 CDT 2017]; root of context hierarchy]
2017-09-07 11:38:39.380 INFO 8212 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown

In above output, the event ApplicationReadyEvent is Spring Boot specific event, which is sent after the context refresh and any related callbacks have been processed to indicate the application is ready to service requests.

Spring boot fires some more additional application events. Some events are actually triggered before the ApplicationContext is created so we cannot receive them using above bean based listener. In the next example, we will see how can we listen to those additional events.

Example Project

Dependencies and Technologies Used :

  • spring-boot-starter 1.5.6.RELEASE: Core starter, including auto-configuration support, logging and YAML.
    Corresponding Spring version: 4.3.10.RELEASE
  • JDK 1.8
  • Maven 3.3.9

Using @EventListener Select All Download
  • boot-with-event-listener-annotation
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources

See Also