The latest need is to execute a method immediately after the project is launched, and then note the four methods found

Annotations @ PostConstruct

Using the @postconstruct annotation is the most common approach, but the problem is that if the method takes too long to execute, the project will not be able to provide services during the method execution.

@component public class StartInit {// // @autoWired can inject bean // ISysUserService userService; @PostConstruct public void init() throws InterruptedException { Thread.sleep(10*1000); System.out.println(123456); system.out.println (123456); }}Copy the code

CommandLineRunner interface

Implement the CommandLineRunner interface and then invoke the required method in the RUN method. The advantage is that by the time the method is executed, the project has already been initialized and can provide services normally.

This method can also accept arguments, which can be handled based on arguments passed in when the project is started: java-jar demo.jar arg1 arg2 arg3. For details, see Spring Boot CommandLineRunner

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

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 parameter passed at startup. CommandLineRunner has no restrictions on the 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 = args.getOptionNames(); for (String optionName : optionNames) { List<String> values = args.getOptionValues(optionName); System.out.println(values.toString()); }}}Copy the code

Realize the ApplicationListener

The ApplicationListener method and the ApplicationRunner and CommandLineRunner interfaces do not affect the service and can provide services normally. It is usually ApplicationStartedEvent or ApplicationReadyEvent. Other events may not inject beans.

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

The order of execution of the four methods

Annotation mode @postconstruct is always implemented first

If you are listening for the ApplicationStartedEvent event, it must be executed before CommandLineRunner and ApplicationRunner.

If you are listening for the ApplicationReadyEvent event, it must be executed after CommandLineRunner and ApplicationRunner.

CommandLineRunner and ApplicationRunner run first by default. If @order is specified for both CommandLineRunner and ApplicationRunner, they are executed in the Order of @Order, with the largest running first.