Execute the method after Springboot starts

1. Annotation @PostConstruct

Using the annotation @PostConstruct is the most common way. The problem is that if the execution method takes too long, the project will not be able to provide services during the method execution.

@Component
public class StartInit {
    
    
//
//    @Autowired   可以注入bean
//    ISysUserService userService;

    @PostConstruct
    public void init() throws InterruptedException {
    
    
        Thread.sleep(10*1000);//这里如果方法执行过长会导致项目一直无法提供服务
        System.out.println(123456);
    }
}

Two, CommandLineRunner interface

Implement the CommandLineRunner interface and then call the method to be called in the run method. The advantage is that when the method is executed, the project has been initialized and the service can be provided normally.

At the same time, this method can also accept parameters, which can be processed according to the parameters passed in when the project starts: java -jar demo.jar arg1 arg2 arg3.

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

3. Implement the ApplicationRunner interface

Implementing the ApplicationRunner interface is basically the same as implementing the CommandLineRunner interface.

The only difference is the format of the parameters passed at startup. CommandLineRunner has no restrictions on the parameter format. The parameter format of the ApplicationRunner interface must be: –key=value

@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
    
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
    
    
        Set<String> optionNames = args.getOptionNames();
        for (String optionName : optionNames) {
    
    
            List<String> values = args.getOptionValues(optionName);
            System.out.println(values.toString());
        }
    }
}

Fourth, implement ApplicationListener

Implementing the ApplicationListener interface and implementing the ApplicationRunner and CommandLineRunner interfaces will not affect the service, and the service can be provided normally. Pay attention to the monitored events, usually ApplicationStartedEvent or ApplicationReadyEvent, and other events may not be injected into the bean.

@Component
public class ApplicationListenerImpl implements ApplicationListener<ApplicationStartedEvent> {
    
    
    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
    
    
        System.out.println("listener");
    }
}

Five, the execution sequence of the four methods

The annotation method @PostConstruct is always executed first

If you listen to the ApplicationStartedEvent event, it must be executed before CommandLineRunner and ApplicationRunner.

If you listen to the ApplicationReadyEvent event, it must be executed after CommandLineRunner and ApplicationRunner.

CommandLineRunner and ApplicationRunner are executed first by ApplicationRunner by default. If @Order is specified by both parties, they will be executed in order of the size of @Order, and the larger one will be executed first.

Guess you like

Origin blog.csdn.net/weixin_44816664/article/details/130045838