[SpringBoot] Five ways to execute methods after startup

After the SpringBoot project is started, there are five ways to execute methods:

1. Implement the CommandLineRunner interface

After the project is initialized, the method will be called and the service will be provided.

@Component
public class StartInit2 implements CommandLineRunner {
    
    

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

2. Implement the ApplicationRunner interface

Same as CommandLineRunner. Only the parameter passing format is different. CommandLineRunner: no restrictions; ApplicationRunner: key-value

@Component
public class StartInit3 implements ApplicationRunner {
    
    

    @Override
    public void run(ApplicationArguments args) {
    
    
        System.out.println("ApplicationRunner=================");
    }
}

3. Implement the ApplicationListener interface

After the project is initialized, the method will be called and the service will be provided. Pay attention to the monitored events, usually ApplicationStartedEvent or ApplicationReadyEvent. Other events may not be injected into the bean.

@Component
public class StartInit4 implements ApplicationListener<ApplicationStartedEvent> {
    
    

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
    
    
        System.out.println("ApplicationListener================ApplicationStartedEvent");
    }
}
  • If the event is listened to ApplicationStartedEvent, ApplicationListener will definitely be executed before CommandLineRunner and ApplicationRunner;
  • If the event is listened to ApplicationReadyEvent, ApplicationListener will definitely be executed after CommandLineRunner and ApplicationRunner;

Order:
The default is ApplicationRunnerto execute first. If both parties specify @Order, then @Orderthe execution will be in order of size, with the smaller ones executed first.

principle:

  1. The run method of SpringApplication will execute the afterRefresh method.
  2. The afterRefresh method will execute the callRunners method.
  3. The callRunners method will call all methods that implement the ApplicationRunner and CommondLineRunner interfaces. The callRunners method will call all methods that implement the ApplicationRunner and CommondLineRunner interfaces.

4. @PostConstruct annotation

This method is called during project initialization. If business logic execution is time-consuming, project startup may fail.

@Component
public class StartInit {
    
    

    @PostConstruct
    public void init() {
    
    
        System.out.println("@PostConstruct===============================");
    }

}

5. Implement the InitializingBean interface

This method is called when the project starts

@Component
public class StartInit6 implements InitializingBean {
    
    

    @Override
    public void afterPropertiesSet() {
    
    
        System.out.println("InitializingBean====================");
    }

}

Guess you like

Origin blog.csdn.net/sco5282/article/details/126365408