How the spring boot command line starts

  • 2021-07-06 11:04:43
  • OfStack

When using spring boot to build application startup, we all start the application through the command line in our work, and sometimes we need 1 specific parameter to do 1 initialization operation when the application starts.

spring boot provides two interfaces, CommandLineRunner and ApplicationRunner, for users to use.

1. CommandLineRunner

1.1 Declare:


@FunctionalInterface
public interface CommandLineRunner {

  /**
   * Callback used to run the bean.
   * @param args incoming main method arguments
   * @throws Exception on error
   */
  void run(String... args) throws Exception;

}

1.2 Use:


package com.example.consoleapplication;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class TestRunner implements CommandLineRunner {

  @Override
  public void run(String... args) {
    // Do something...
    for(String arg: args){
      System.out.println(arg);
    }
    System.out.print("test command runner");
  }
}

1.3 Operational Results

Running: java -jar build/libs/consoleapplication-0.0.1-SNAPSHOT.jar -sdfsaf sdfas,

The results are as follows:

2019-03-16 17:31:56.544 INFO 18679 --- [ main] c.e.consoleapplication.DemoApplication : No active profile set, falling back to default profiles: default
2019-03-16 17:31:57.195 INFO 18679 --- [ main] c.e.consoleapplication.DemoApplication : Started DemoApplication in 16.172 seconds (JVM running for 16.65)
-sdfsaf
sdfas
test command runner%

2. ApplicationRunner

2.1 Statement


/**
 * Interface used to indicate that a bean should <em>run</em> when it is contained within
 * a {@link SpringApplication}. Multiple {@link ApplicationRunner} beans can be defined
 * within the same application context and can be ordered using the {@link Ordered}
 * interface or {@link Order @Order} annotation.
 *
 * @author Phillip Webb
 * @since 1.3.0
 * @see CommandLineRunner
 */
@FunctionalInterface
public interface ApplicationRunner {

  /**
   * Callback used to run the bean.
   * @param args incoming application arguments
   * @throws Exception on error
   */
  void run(ApplicationArguments args) throws Exception;

}

2.2 Use

The use of ApplicationRunner and CommandLineRunner is different:

The use of CommandLineRunner only divides parameters according to spaces. ApplicationRunner parses parameters based on whether they match--key=value, If it can match, it is optional parameter, and getOptionValues can be used to obtain parameter value. A mismatch is the non optional parameter.

package com.example.consoleapplication;

import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.boot.ApplicationArguments;

@Component
public class TestApplicationRunner implements ApplicationRunner {

  @Override
  public void run(ApplicationArguments args) throws Exception {
    // Do something...
    System.out.println("option arg names" + args.getOptionNames());
    System.out.println("non option+" + args.getNonOptionArgs());
  }
}

2.3 Operational Results

Run command java -jar build/libs/consoleapplication-0.0.1-SNAPSHOT.jar -non1 non2 --option=1 The result is:

2019-03-16 18:08:08.528 INFO 19778 --- [ main] c.e.consoleapplication.DemoApplication : No active profile set, falling back to default profiles: default
2019-03-16 18:08:09.166 INFO 19778 --- [ main] c.e.consoleapplication.DemoApplication : Started DemoApplication in 16.059 seconds (JVM running for 16.56)
test
option arg names[option]
non option+[-non1, non2]-non1
non2
--option=1
test%

As you can see, the parameter name of optional is option, and the parameters of non and optional are-non1 and non2

3. Summary

CommandLineRunner and ApplicationRunner can achieve command-line application startup according to the parameters we need to get the value, do special logic. However, they are different, so it is recommended to use optional parameters of ApplicationRunner for easy expansion.

4. Reference documentation

https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#boot-features-web-environment


Related articles: