SpringBoot event listening

Keywords: Spring Programming SpringBoot


The Spring framework provides a perfect event monitoring mechanism. The steps to implement event monitoring in the Spring framework are as follows:

  1. Custom events, inheriting org.springframework.context.ApplicationEvent abstract class
  2. Define event listener, implement org.springframework.context.ApplicationListener interface
  3. Publishing events in the Spring container

There are three ways to listen to events in the Spring framework:

  • Programming: listening for events of the specified type by implementing the ApplicationListener interface
  • Annotative: listen for events of the specified parameter type by adding @ EventListener annotation to the method. Writing this class needs to be hosted in the Spring container
  • Configuration: through application.properties Medium configuration context.listener.classes Property specifies the listener


Create a SpringBoot project and add dependencies:




Step 1: customize the event (simulate mail sending):

public class EmailEvent extends ApplicationEvent {
    public EmailEvent(Object source) {

Step 2: customize the event listener:

public class EmailEventListener implements ApplicationListener<EmailEvent> {
    public void onApplicationEvent(EmailEvent event) {//Parameter indicates the type of event to listen to
        System.out.printf("Event detected %s : start-%s \n" 

Step 3: create Controller:

public class DemoController {
    private ApplicationContext act;
    //Publishing events
    public void pubEvnet() {
        //TODO: business processing
        System.out.println("Publishing events");
        act.publishEvent(new EmailEvent("Send mail"));
        //TODO: business processing

Deployment project, web access, console view results:

Improvement 1

Step 2: the annotation @ Component of the custom event listener class can be omitted, and then the annotation can be added manually in the project startup class. The modified startup class code is as follows:

public class EventDemoApplication{//corresponding com.hc.demo1 package
    public static void main(String[] args) {
        ConfigurableApplicationContext cat = 
		SpringApplication.run(EventDemoApplication.class, args);
        cat.addApplicationListener(new EmailEventListener1());//Add Monitor Manually

Improvement 2:

Step 2: the user-defined event listener class does not need to implement the ApplicationListener interface. Add the annotation @ EventListener to specify the listening method and the event of parameter type. Writing this class needs to be hosted in the Spring container.

public class EmailEventListener2 { //No interfaces need to be inherited
    public void onApplicationEvent(EmailEvent2 event) {//Parameter indicates the type of event to listen to
        System.out.printf("Monitored %s : event-%s", event.getClass().getName(), event.getSource());

Then use the default system startup class.


The so-called configuration means that application.yml Configuration in file context.listener.classes Property specifies the event listener. The example code is as follows:

        classes: com.hc.demo3.EmailEventListener3

Posted by phpsir on Tue, 09 Jun 2020 01:51:07 -0700