SpringBoot - implements the execution of specified tasks at startup (CommandLineRunner, ApplicationRunner)

Posted by assafbe on Mon, 03 Jan 2022 19:06:56 +0100

Sometimes some special tasks need to be executed when the system starts, such as configuration file loading, database initialization and so on. Spring Boot , provides two solutions: CommandLineRunner , and , ApplicationRunner. The two methods of use are generally the same, and the difference is mainly reflected in the parameters.
1, Using CommandLineRunner

1. Basic introduction

When the Spring Boot , project starts, it will traverse all the implementation classes of , CommandLineRunner , and call the , run , method.
  • If there are multiple implementation classes of "CommandLineRunner" in the whole system, you can use the @ Order annotation to sort the call Order of these implementation classes (the smaller the number, the first to execute).
  • The parameters of the run method are the parameters passed in from the system startup, that is, the parameters of the main method in the entry class (passed into the Spring Boot project when calling the spring application.run method)
 

2. Use examples

(1) First, add two CommandLineRunner classes to the project and create two new classes. Their contents are as follows: print the parameters passed in at startup:
@Component
@Order(1)
public class MyCommandLineRunner1 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("Runner1>>>"+ Arrays.toString(args));
    }
}
 
@Component
@Order(2)
public class MyCommandLineRunner2 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("Runner2>>>"+ Arrays.toString(args));
    }
}

(2) We can configure the parameters that need to be passed in during system startup. Here, take "intelliJ IDEA" as an example, click Edit startup configuration in the upper right corner.

 

(3) Edit the "Program arguments" column in the pop-up page and fill in the parameters to be passed in. If there are multiple parameters, the parameters are separated by spaces. Here we have both option parameters and non option parameters.
If we package the project and run it as a {jar} package. Then these parameters can follow the startup command:
java -jar springboot-demo-0.0.1-SNAPSHOT.jar --name=zhangsan --age=25 zhangsan1 251

(4) Start the project, and the console output is as follows:

2, Using ApplicationRunner

1. Basic introduction

(1) The usage of ApplicationRunner is basically the same as that of CommandLineRunner. When the project starts, it will traverse all the implementation classes of application runner and call the run method.
If there are multiple ApplicationRunner implementation classes in the whole system, you can also use the @ Order annotation to sort the call Order of these implementation classes (the smaller the number, the first to execute).
 
(2) The difference between ApplicationRunner , and , CommandLineRunner , is mainly reflected in the parameters of the , run , method. Unlike the array parameter of the run method in the CommandLineRunner, the parameter of the run method in the ApplicationRunner is an ApplicationArguments object.
ApplicationArguments distinguishes between option parameters and non option parameters:
  • For non option parameters: we can get them through the getNonOptionArgs() method of # ApplicationArguments # and get an array.
  • For option parameters: you can get all option names through the getOptionNames() method of # ApplicationArguments #. Get the actual value through the getOptionValues() method (which returns a list string).

2. Use examples

(1) First, add two} applicationrunners to the project. Their contents are as follows: print the parameters passed in during startup:
@Component
@Order(1)
public class MyApplicationRunner1 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<String> nonOptionArgs = args.getNonOptionArgs();
        System.out.println("Runner1[Non option parameters]>>> " + nonOptionArgs);
        Set<String> optionNames = args.getOptionNames();
        for(String optionName: optionNames) {
            System.out.println("Runner1[Option parameters]>>> name:" + optionName
                    + ";value:" + args.getOptionValues(optionName));
        }
    }
}
 
@Component
@Order(2)
public class MyApplicationRunner2 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<String> nonOptionArgs = args.getNonOptionArgs();
        System.out.println("Runner2[Non option parameters]>>> " + nonOptionArgs);
        Set<String> optionNames = args.getOptionNames();
        for(String optionName: optionNames) {
            System.out.println("Runner2[Option parameters]>>> name:" + optionName
                    + ";value:" + args.getOptionValues(optionName));
        }
    }
}

(2) Start the project, and the console output is as follows: