ApplicationRunner interface and CommandLineRunner interface in [SpringBoot]

1. ApplicationRunner interface

usage:

  • Type: interface

  • Method: Only one run method is defined

  • Usage scenarios: When the springBoot project is started, if you want to execute a certain piece of code directly after startup, you can use the ApplicationRunner interface, implement the run(ApplicationArguments args) method in the interface, and write your own code logic in the method. That is: after the spring container is started, the run method of this interface implementation class will be executed immediately.

  • Parameters of the run method: ApplicationArguments can obtain the command parameters executed by the current project. (For example, when you type this project into a jar and execute it, the parameters can be obtained through ApplicationArguments);

@Component  //此类一定要交给spring管理
public class ConsumerRunner implements ApplicationRunner{
    
    
     @Override
     public void run(ApplicationArgumers args) throws Exception{
    
    
        //代码
        System.out.println("需要在springBoot项目启动时执行的代码---");
     }
}

If there are multiple code segments that need to be executed, the @Order annotation can be used to set the order of execution.

In the same project, you can define multiple ApplicationRunner implementation classes, and their execution order is achieved by annotating the @Order annotation or implementing the Ordered interface.
@Order annotation: If there are multiple implementation classes and you need them to be executed in a certain order, you can add the @Order annotation to the implementation class. @Order(value=integer value). SpringBoot will execute the values ​​in @Order from small to large.

@order, use annotations to control the loading order of beans; the @Order tag defines the loading order of components. The smaller the value, the higher the priority, and it can be a negative number. The smaller the value, the earlier it is loaded.
@Order(-1) takes precedence over @Order(0)
@Order(1) takes precedence over @Order(2)

@Component  //此类一定要交给spring管理
@Order(value=1) //首先执行
public class ConsumerRunnerA implements ApplicationRunner{
    
    
    @Override
    public void run(ApplicationArgumers args) throws Exception{
    
    
        //代码
        System.out.println("需要在springBoot项目启动时执行的代码1---");
    }
}
@Component  //此类一定要交给spring管理
@Order(value=2) //其次执行
public class ConsumerRunnerB implements ApplicationRunner{
    
    
    @Override
    public void run(ApplicationArgumers args) throws Exception{
    
    
        //代码
        System.out.println("需要在springBoot项目启动时执行的代码2---");
    }
}

@Component annotation: Inject the class into the spring container.

Reference link: https://blog.csdn.net/hc1285653662/article/details/122445495

2. CommandLineRunner interface

usage:

Write a class in the last callback step after the container is successfully started (similar to self-starting at boot)
, implement the CommandLineRunner interface, and inject this class into the Spring container;

The execution order can be controlled through the @Order annotation (the smaller the number specified by the attribute, the higher the priority) or the Ordered interface.

EX: Customize two classes, implement the CommandLineRunner interface, implement the run method, and add processing logic in the run method.

@Order(5)
@Component
public class AppStartReport implements CommandLineRunner {
    
    

    @Override
    public void run(String... args) throws Exception {
    
    
        System.out.println("AppStartReport : 项目成功启动------------------");
    }
}
@Order(2)
@Component
public class AppStartReport2 implements CommandLineRunner {
    
    

    @Override
    public void run(String... args) throws Exception {
    
    
        System.out.println("AppStartReport2 : 项目成功启动------------------");
    }
}

Startup effect: AppStartReport2 is executed before AppStartReport:
Insert image description here

3. The difference between the ApplicationRunner interface and the CommandLineRunner interface

the difference:

  1. The method parameters of CommandLineRunner are original parameters without any processing;
  2. The parameters of ApplicationRunner are ApplicationArguments objects, which are further encapsulation of the original parameters. ApplicationArguments further processes the parameters (mainly for the main method). It can parse the parameters of –name=value, and we can obtain the value based on the name.
@Component
public class AppStartReport3 implements ApplicationRunner {
    
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
    
    
        System.out.println("AppStartReport3 : 项目成功启动,参数为: " + Arrays.asList(args.getSourceArgs()));
    }
}
@SpringBootApplication
public class App {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication application = new SpringApplication(App.class);
        ConfigurableApplicationContext context = application.run("aaa", "bbb");
        context.close();
    }
}

After specifying the parameters yourself, the execution effect is as follows:
Insert image description here

You can also set the initial value in the Program arguments option box, in the form of “–key=value”, such as “–argname=ccc”, as shown in the following figure:
Insert image description here

@SpringBootApplication
public class App {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication application = new SpringApplication(App.class);
        ConfigurableApplicationContext context = application.run(args);
        context.close();
    }
}

The parameters passed in by the launcher are the parameter args of the main method, and the effect is as follows:
Insert image description here

ApplicationArguments source code analysis

public interface ApplicationArguments {
    
    

   String[] getSourceArgs();

   Set<String> getOptionNames();

   boolean containsOption(String name);

   List<String> getOptionValues(String name);

   List<String> getNonOptionArgs();

}

There are two types of resource parameters, one is passed in when calling the run method ("aaa", "bbb"), and the other is the configured system parameter, which is the args parameter of the main method ("ccc").

  • First, the getSourceArgs method can obtain all parameters, which can be parameters passed in by yourself or configured system parameters;

  • The getNonOptionArgs method can get the parameters we passed in ("aaa", "bbb"). ;

  • The remaining three methods can obtain system parameters. The actual usage is as follows:
    EX:

@SpringBootApplication
public class App {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication application = new SpringApplication(App.class);
        ConfigurableApplicationContext context = application.run(args);
        ApplicationArguments arguments = context.getBean(ApplicationArguments.class);
        System.out.println("参数个数:" + arguments.getSourceArgs().length);
        System.out.println("Non OptionArgs:" + arguments.getNonOptionArgs());
        System.out.println("Option Names:" + arguments.getOptionNames());
        System.out.println("获取key为argname的value值:" + arguments.getOptionValues("argname"));
        System.out.println("是否包含key为argname的参数:" + arguments.containsOption("argname"));
        context.close();
    }
}

Output effect:
Insert image description here

Reference link: https://blog.csdn.net/qq_35006663/article/details/102264172

Guess you like

Origin blog.csdn.net/m0_46459413/article/details/132322256