Callback commonly used in spring boot startup

Posted by Maggan on Sun, 09 Jan 2022 16:32:04 +0100

1. Introduction

springboot provides very rich callback interfaces, which can be used to do many things. Some common callback interfaces are introduced

2. Common expansion interfaces

1.ApplicationContextInitializer
2.ApplicationListener
3.ApplicationRunner
4.CommandLineRunner

3. Examples:

The 1.ApplicationContextInitializer interface is called before the spring container is initialized.

Create the MyApplicationContextInitializer class to implement the ApplicationContextInitializer interface

public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        System.out.println("MyApplicationContextInitializer Start..........");
    }
}

Because the interface is before the container is initialized, the annotation is useless. Initialize the MyApplicationContextInitializer class in the mian method

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(Application.class);
        springApplication.addInitializers(new MyApplicationContextInitializer());
        springApplication.run(args);
        //SpringApplication.run(Application.class, args);
    }
}

2.ApplicationListener listener. This interface can trigger different conditions according to different events

spring container built-in trigger events:

  • ContextRefreshedEvent: event raised when initializing or refreshing ApplicationContext
  • ContextStartedEvent: the event raised when ApplicationContext is started
  • ContextStoppedEvent: event raised when ApplicationContext stops
  • ContextClosedEvent: event raised when ApplicationContext is closed

Code example:

@Component
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("MyApplicationListener. . . . . . . . . . ");
    }
}

Note: spring also has an ApplicationEvent interface to customize events

3. The trigger time of applicationrunner is after the container is loaded

@Component
public class MyApplicationRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("MyApplicationRunner. . . . . . . . . . ");
    }

}

Source code:

/**
 *Used to indicate that the bean contains the spring application that should run when the spring application
 *Multiple applicationrunner beans can be defined in the same application context and can be sorted using the Ordered interface or the @ Order annotation.
 */
public interface ApplicationRunner {

    /**
     * Incoming application parameters
     */
    void run(ApplicationArguments args) throws Exception;

}

4.CommandLineRunner

The function is basically the same as that of ApplicationRunner. The difference is that the parameter type of the interface is the original String array passed here. The extensible ApplicationRunner will be better

@Component
public class MyCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("MyCommandLineRunner. . . . . . . . . . ");
    }

}

4. Other expansion interfaces:

1.InstantiationAwareBeanPostProcessor

Add the BeanPostProcessor sub interface of the callback before instantiation, and the callback after instantiation but before setting explicit properties or automatic assembly.
It is usually used to suppress the default instantiation of a specific target bean, such as creating a proxy with special TargetSources (pool target, delayed initialization target, etc.), or implementing other injection strategies, such as field injection.
Note: this interface is a special interface, which is mainly used inside the framework. It is recommended to implement the ordinary BeanPostProcessor interface as much as possible, or derive from the instantiaawarebeanpostprocessoradapter to mask the extension of this interface.

2.SmartInstantiationAwareBeanPostProcessor

The extension of the instantiaawarebeanpostprocessor interface adds a callback to predict the final type of processed beans.
Note: this interface is a special interface, which is mainly used inside the framework. Generally, the post processor provided by the application should simply implement the ordinary BeanPostProcessor interface or derive from the instantiaawarebeanpostprocessoradapter class.

Thank you for reading to the end. If there are any errors, please correct them.

Topics: Java