SoFunction
Updated on 2025-03-08

Four ways to execute a method immediately after Springboot is started

The latest method needs to be executed immediately after the project is started, and then we will record the four methods found here.

Annotation @PostConstruct

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

@Component
public class StartInit {
//
// @Autowired can inject beans//    ISysUserService userService;

    @PostConstruct
    public void init() throws InterruptedException {
        (10*1000);// If the method is executed too long, the project will not be able to provide services.        (123456);
    }
}

CommandLineRunner interface

Implement the CommandLineRunner interface and then call the method that needs 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, and can perform some processing based on the parameters passed in at the project startup: java -jar arg1 arg2 arg3. See:Spring boot CommandLineRunner starts task transfer parameters

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

Implement the ApplicationRunner interface

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

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

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

Implement ApplicationListener

Implementing the ApplicationListener interface and implementing the ApplicationRunner and the CommandLineRunner interface do not affect the service, and they can provide services normally. Pay attention to the events that are listened to, usually the ApplicationStartedEvent or the ApplicationReadyEvent. Other events may not be injected into the bean.

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

Four ways of execution order

Annotation method @PostConstruct always executes first

If the ApplicationStartedEvent event is being listened to, it will be executed before CommandLineRunner and ApplicationRunner.

If the ApplicationReadyEvent event is listening, it will be executed after CommandLineRunner and ApplicationRunner.

CommandLineRunner and ApplicationRunner default to ApplicationRunner to execute first. If both parties specify @Order, they will execute in the order of @Order size, and the larger ones will execute first.

Summarize

This is the introduction to this article about four ways to execute a certain method immediately after Springboot is started. For more relevant content on execution methods after Springboot is started, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!