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

Posted by atrum on Tue, 28 Sep 2021 18:43:28 +0200

Narration

Sometimes special tasks need to be performed at system startup, such as configuration file loading, database initialization, etc. Spring Boot provides two solutions: CommandLineRunner and ApplicationRunner. They are used in a similar way, and the differences are mainly reflected in the parameters.


Solution

CommandLineRunner

1. Introduction

The Spring Boot project starts by traversing all the implementation classes of the CommandLineRunner and calling its run method.

  • If there are multiple CommandLineRunner implementation classes in the system, you can use the @Order annotation to sort the order in which these implementation classes are called (the smaller the number, the earlier they are executed).
  • The parameter to the run method is that the system startup is the passed parameter, that is, the parameter to the main method in the entry class (passed into the Spring Boot project when the SpringApplication.run method is called)

2, Use sample

(1) First add two CommandLineRunner s to the project, which are printed out with 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 at system startup. Take intelliJ IDEA for 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 that need to be passed in. If there are multiple parameters, they are separated by spaces. Here we have both option and non-option parameters.

If we package the project and run it as a jar package, these parameters can follow the start command:

  • java -jar demo-0.0.1-SNAPSHOT.jar --name=hangge --age=100


(4) Start the project with the console output as follows:

ApplicationRunner

1. Introduction

(1) ApplicationRunner usage is basically the same as CommandLineRunner. The project will start by traversing all the implementation classes of ApplicationRunner and calling the run method.

If there are multiple implementation classes of the ApplicationRunner throughout the system, you can also use the @Order annotation to sort the order in which these implementation classes are called (the smaller the number, the earlier they are executed).

(2) The difference between ApplicationRunner and CommandLineRunner is mainly reflected in the parameters of the run method. Unlike the array parameters of the run method in CommandLineRunner, the parameter of the run method in ApplicationRunner is an ApplicationArguments object.

ApplicationArguments distinguishes between option and non-option parameters:

  • For non-option parameters: we can get it from the getNonOptionArgs() method of ApplicationArguments, which is an array.
  • For option parameters: All option names can be obtained by the getOptionNames() method of ApplicationArguments. The actual values are obtained by the getOptionValues() method (which returns a list string).

2, Use sample

(1) First add two ApplicationRunner s to the project, which are printed out with the parameters passed in at 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-Optional 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-Optional Parameters]>>> " + nonOptionArgs);
        Set<String> optionNames = args.getOptionNames();
        for(String optionName: optionNames) {
            System.out.println("Runner2[Option parameters]>>> name:" + optionName
                    + ";value:" + args.getOptionValues(optionName));
        }
    }
}

The original text is from: www.hangge.com  Reload with original link: https://www.hangge.com/blog/cache/detail_2508.html

(2) We can configure the parameters that need to be passed in at system startup. Take intelliJ IDEA for 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 that need to be passed in. If there are multiple parameters, they are separated by spaces. Here we have both option and non-option parameters.

If we package the project and run it as a jar package, these parameters can follow the start command:

  • java -jar demo-0.0.1-SNAPSHOT.jar --name=hangge --age=100 


(4) Start the project with the console output as follows:

Topics: Java Spring Spring Boot