Home  >  Article  >  Java  >  Spring boot CommandLineRunner starts task parameter passing method

Spring boot CommandLineRunner starts task parameter passing method

WBOY
WBOYforward
2023-05-12 12:43:061237browse

Preface

Some people may think, what are the args parameters in the run(String... args) method?

@Component
@Order(value = 1) // 指定其执行顺序,值越小优先级越高
public class MyRunner1 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("MyRunner1");
    }
}

String... args are parameters that can be passed in when the application starts. There are two ways to pass parameters.

One is to pass parameters from the command line, so why is this interface Called CommandLineRunner

Another method is to configure parameters through IntelliJ IDEA

The following are explained separately

Command line parameter passing

First package the application into a jar package , and then run the following command line. I pass in three parameters here

java -jar MyProject.jar 野野新一 野元新二 野元新三

IntelliJ IDEA Pass Parameter

If you want to run the project directly through IntelliJ IDEA during the development process, and do not want to package it into a jar package and pass in parameters, you can configure the environment in which the project runs

1. Click Edit Configurations ...Open the project running configuration dialog box

Spring boot CommandLineRunner starts task parameter passing method

2 Expand Environment, fill in the project running parameters in the Program arguments item, and click the OK button to confirm

Spring boot CommandLineRunner starts task parameter passing method

Test

We slightly modify the above example and print out the parameter args

@Component
@Order(value = 1) // 指定其执行顺序,值越小优先级越高
public class MyRunner1 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("MyRunner1:" + Arrays.toString(args));
    }
}

Use the above command line method or the IntelliJ IDEA configuration parameter method The running results are the same, as follows

2020-08-21 16:36:04.453 custom-logback INFO 16244 --- [ main] com.yeyuanxinyi.MyApplication : Started MyApplication in 10.724 seconds (JVM running for 13.727)
MyRunner1:[Nozaru Shinichi, Nozaru Shinji, Nozaru Shinsan]

The above is the detailed content of Spring boot CommandLineRunner starts task parameter passing method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete