Analysis of ApplicationContext event mechanism

ApplicationContext event mechanism is the implementation of observer design pattern. ApplicationContext event processing can be realized through ApplicationEvent class and ApplicationListener interface.   If there is an applicationlister bean in the container, the applicationlister bean will be triggered automatically whenever ApplicationContext publishes an ApplicationEvent. This event mechanism must be triggered by the program display. spring has some built-in events. When an operation is completed, some event actions will be issued. For example, listen for the ContextRefreshedEvent event. This event will be triggered when all beans are initialized and loaded successfully. Implement the ApplicationListener interface to receive the listening action, and then write your own logic. Similarly, events can be customized, and listening can also be customized. They can be processed completely according to their own business logic.

Built in events

Serial number
Spring built-in event & Description
ApplicationContext   This event is published when it is initialized or refreshed. This can also be done in   The refresh() method is used in the ConfigurableApplicationContext interface. Initialization here means that all beans are successfully loaded, post-processing beans are detected and activated, all singleton beans are pre instantiated, and the ApplicationContext container is ready and available
When used   ConfigurableApplicationContext   (ApplicationContext sub interface) the start() method in the interface starts   ApplicationContext   This event is published when. You can investigate your database, or you can restart any stopped application after receiving this event.
When used   ConfigurableApplicationContext   stop() in the interface stops   ApplicationContext   Publish this event when. You can do the necessary cleaning up after receiving this event.
When used   ConfigurableApplicationContext   The close() method in the interface is closed   ApplicationContext   This event is published when. A closed context reaches the end of the life cycle; It cannot be refreshed or restarted.
This is a Web specific event that tells all beans that HTTP requests have been serviced. Can only be applied to Web applications that use DispatcherServlet. When Spring is used as the front-end MVC controller, the system will automatically trigger this event when Spring finishes processing user requests.

Examples of business party listening events

For example, if you want to listen for the event of ContextRefreshedEvent, you can implement the ApplicationListener interface and pass in the event to listen
public class TestApplicationListener implements ApplicationListener<ContextRefreshedEvent>{
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

Custom event

You can customize events, and then issue them manually after business processing. A user-defined event inherits a listening interface. The listening class will process the business after receiving the event.

Event definition

public class EmailEvent extends ApplicationEvent{
   private String address;
   private String text;
   public EmailEvent(Object source, String address, String text){
      this.address = address;
      this.text = text;
   public EmailEvent(Object source) {
   //......address and text of setter,getter

Listening definition

public class EmailNotifier implements ApplicationListener{
   public void onApplicationEvent(ApplicationEvent event) {
     if (event instanceof EmailEvent) {
        EmailEvent emailEvent = (EmailEvent)event;
        System.out.println("Email address:" + emailEvent.getAddress());
        System.our.println("Message content:" + emailEvent.getText());
     } else {
        System.our.println("Container itself events:" + event);

Service trigger

public class SpringTest {
   public static void main(String args[]){
     ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
     //Create a ApplicationEvent object
     EmailEvent event = new EmailEvent("hello","","This is a test");
     //Actively trigger the event

Posted by zab329 on Fri, 03 Dec 2021 03:47:02 -0800