SpringBoot event listening

Keywords: Spring Programming SpringBoot

summary

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

Programming

Create a SpringBoot project and add dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Programming

Basics

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

public class EmailEvent extends ApplicationEvent {
    public EmailEvent(Object source) {
        super(source);
    }
}

Step 2: customize the event listener:

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

Step 3: create Controller:

@RestController
public class DemoController {
    @Resource
    private ApplicationContext act;
    //Publishing events
    @RequestMapping("/pubEvnet")
    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:

@SpringBootApplication
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.

@Component
public class EmailEventListener2 { //No interfaces need to be inherited
    @EventListener
    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.

Configurable

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:

context:
    listener:
        classes: com.hc.demo3.EmailEventListener3

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