How SpringBoot executes code immediately after starting the project

1. Call InitializingBean when the project starts

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
 
@Component
public class InitializingBeanImpl implements InitializingBean {
    
    
 
    @Override
    public void afterPropertiesSet() throws Exception {
    
    
        System.out.println("InitializingBeanImpl方法执行");
    }
}

2. Call @PostConstruct during project initialization

import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
 
@Component
public class PostConstructTest {
    
    
    @PostConstruct
    public void start() {
    
    
        System.out.println("@PostConstruct方法执行");
    }
 
    @PostConstruct
    public void start01() {
    
    
        System.out.println("@PostConstruct1111方法执行");
    }
}

You can write multiple methods in a class.

3. After the project is initialized, call CommandLineRunner, ApplicationRunner, and ApplicationListener

CommandLineRunner

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
 
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
    
    
    @Override
    public void run(String... args) throws Exception {
    
    
        System.out.println("CommandLineRunnerImpl方法执行");
    }
}

ApplicationRunner

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
 
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
    
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
    
    
        System.out.println("ApplicationRunner方法执行");
    }

The implementation method of both is the same, which is to inherit the corresponding interface and then rewrite the run method. They are also interfaces provided to us by the SpringBoot framework. They are also the most commonly used in projects. They are more flexible and have multiple CommandLineRunner or When ApplicationRunner implements a class, it can override the getOrder method through the @Order annotation or implement the Ordered interface to implement execution in the specified order.

@Component
@Order(1) //通过order注解指定
public class ApplicationRunnerImpl implements ApplicationRunner {
    
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
    
    
        System.out.println("ApplicationRunner方法执行");
    }
}

By implementing the Ordered interface and overriding the getOrder method, the smaller the number, the first it will be executed.

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
 
@Component
public class ApplicationRunnerImpl implements ApplicationRunner, Ordered {
    
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
    
    
        System.out.println("ApplicationRunner方法执行");
    }
 
    @Override
    public int getOrder() {
    
    
        return 1;
    }
}


After the ApplicationListener 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 you are listening to the ApplicationStartedEvent event, ApplicationListener will definitely be executed before CommandLineRunner and ApplicationRunner;

If you are listening to the ApplicationReadyEvent event, ApplicationListener will definitely be executed after CommandLineRunner and ApplicationRunner;

Order:
By default, ApplicationRunner executes first. If both parties specify @Order, they will be executed in order of size of @Order, with smaller ones executed first.

principle:

The run method of SpringApplication will execute the afterRefresh method.
The afterRefresh method will execute the callRunners method.
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.

Guess you like

Origin blog.csdn.net/weixin_42774617/article/details/131775398