[Yugong Series] Java Teaching Course 153-SpringBoot Event Listening in July 2023


foreword

A Spring Boot event listener is a mechanism for listening to events and state changes in an application. It allows developers to receive notifications and perform corresponding actions when the application starts, closes, fails, or other events occur. SpringBoot provides a variety of event listeners, such as application startup events, application shutdown events, web server startup events, web server shutdown events, and more. Developers can monitor the triggering of these events and perform corresponding operations by implementing a specific listener interface and registering it in SpringBoot.

SpringBoot event listening is a mechanism for listening to state changes and events in the application. An event listener is a programming pattern for executing custom code when some state change or event occurs in the application. Spring Boot provides some commonly used event listeners, here are a few:

  1. ApplicationStartingEvent: An event when the application is starting. Some initialization operations can be performed before the application starts.

  2. ApplicationEnvironmentPreparedEvent: Event when the application's environment is ready. You can get the environment information of the application and do some configuration in this event.

  3. ApplicationPreparedEvent: The event when the application is ready. Some complex initialization work can be performed in this event.

  4. ApplicationStartedEvent: Event when the application has started. You can perform some background tasks or start asynchronous threads in this event.

  5. ApplicationReadyEvent: The event when the application is ready to accept requests. Some business logic of the application can be executed in this event.

  6. ApplicationFailedEvent: The event when the application fails to start. You can do some exception handling or start an alternate service in this event.

In addition to the above commonly used event listeners, Spring Boot also provides many other event listeners, such as ServletWebServerInitializedEvent, ContextRefreshedEvent, ContextClosedEvent, etc. Developers can choose corresponding event listeners to register according to their own needs.

1. SpringBoot event monitoring

The event listening mechanism in Java defines the following roles:

①Event: Event, an object that inherits from the java.util.EventObject class

②Event source: Source, any object Object

③Listener: Listener, an object that implements the java.util.EventListener interface

SpringBoot will call back several listeners when the project starts. We can implement these listener interfaces and complete some operations when the project starts.

  • ApplicationContextInitializer、

  • SpringApplicationRunListener、

  • CommandLineRunner、

  • ApplicationRunner

    The start timing of the custom listener: MyApplicationRunner and MyCommandLineRunner are both executed after the project starts, and can be used by putting them into the container using @Component

MyApplicationRunner

/**
 * 当项目启动后执行run方法。
 */
@Component
public class MyApplicationRunner implements ApplicationRunner {
    
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
    
    
        System.out.println("ApplicationRunner...run");
        System.out.println(Arrays.asList(args.getSourceArgs()));
    }
} 

MyCommandLineRunner

@Component
public class MyCommandLineRunner implements CommandLineRunner {
    
    
    @Override
    public void run(String... args) throws Exception {
    
    
        System.out.println("CommandLineRunner...run");
        System.out.println(Arrays.asList(args));
    }
}

The use of MyApplicationContextInitializer needs to add META-INF/spring.factories under the resource folder

org.springframework.context.ApplicationContextInitializer=com.itheima.springbootlistener.listener.MyApplicationContextInitializer
@Component
public class MyApplicationContextInitializer implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        System.out.println("ApplicationContextInitializer....initialize");
    }
}

The use of MySpringApplicationRunListener needs to add a constructor

public class MySpringApplicationRunListener implements SpringApplicationRunListener {

    public MySpringApplicationRunListener(SpringApplication application, String[] args) {
    }

    @Override
    public void starting() {
        System.out.println("starting...项目启动中");
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        System.out.println("environmentPrepared...环境对象开始准备");
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("contextPrepared...上下文对象开始准备");
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        System.out.println("contextLoaded...上下文对象开始加载");
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        System.out.println("started...上下文对象加载完成");
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        System.out.println("running...项目启动完成,开始运行");
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        System.out.println("failed...项目启动失败");
    }
}

Guess you like

Origin blog.csdn.net/aa2528877987/article/details/131545515