Functions and differences between commandlinerunner and applicationrunner -- springboot

Programmer light 2022-02-13 07:52:39 阅读数:426

functions differences commandlinerunner applicationrunner springboot

One 、 Requirements and preface

springBoot Framework projects , Sometimes there is a need to preload data —— Load in advance into the cache or in the properties of the class , And the time when you want to perform the operation is at the end of the container startup . For this scenario ,SpringBoot Two interfaces are provided , Namely CommandLineRunner and ApplicationRunner. Both interfaces are spring-boot Of jar In bag (spring-boot Of jar Package attachment :spring-boot<-spring-boot-starter<-spring-boot-starter-web), The project only needs to rely on spring-boot-starter-web Of jar You can use .

Two 、 Use of interfaces

2.1、ApplicationRunner Interface to use

2.1.1、 New class implementation ApplicationRuner Interface , And add notes @Component Let the container scan to . The following sample code :

package com.runner.runner;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(" Start preloading data (ApplicationRunner)...");
args.getOptionNames().forEach(System.out::println);
Arrays.stream(args.getSourceArgs()).forEach(System.out::println);
}
}

 2.2.2、 Start class code :

package com.runner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RunnerApplication {
public static void main(String[] args) {
SpringApplication.run(RunnerApplication.class, args);
System.out.println("RunnerApplication completion of enforcement ");
}
}

2.2.3、 Screenshot of key information of startup log ( Be careful : Preload data after the container starts , Start execution before custom log printing .)

 

2.2、CommandLineRunner Interface to use

2.2.1、 New class implementation CommandLineRuner Interface , And add notes @Component Let the container scan to . The following sample code :

package com.runner.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(" Start preloading data (CommandLineRunner)...");
Arrays.stream(args).forEach(System.out::println);
}
}

2.2.2、 Start class code :

package com.runner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RunnerApplication {
public static void main(String[] args) {
SpringApplication.run(RunnerApplication.class, args);
System.out.println("RunnerApplication completion of enforcement ");
}
}

2.2.3、 Screenshot of key information of startup log ( Be careful : Preload data after the container starts , Start execution before custom log printing .)

3、 ... and 、 difference

3.1、 The implementation methods of the two interfaces are the same , The parameters are different , The others are no different . Both parameters can receive java Parameters and values set by the command , as follows

ApplicatonRunner The implementation class needs to implement the methods :

@Override
public void run(ApplicationArguments args) {}

CommandLineRunner The methods that need to be implemented by the implementation class

@Override
public void run(String... args) {}

3.1.1、 Set command line parameters :

--spring.profile.active=test, but ApplicatonRunner Method parameters of interface ApplicationArguments( It's an object ) Than CommandLineRunner Method parameters of interface ( Is a can receive multiple string Parameters of ) More powerful .ApplicatonRunner Method parameters of interface ApplicationArguments You can get the string of parameters , It can also be obtained directly key;CommandLineRunner The method parameter of the interface can only get the string of the parameter .

Screenshot of Key log information :

3.2、ApplicationRunner Interface implementation method ratio CommandLineRunner Before the implementation method of the interface ( Of course you can set it @Order To determine who executes first ), Here's the picture .

package com.runner.runner;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Order(5)
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) {
System.out.println(" Start preloading data (ApplicationRunner)...");
args.getOptionNames().forEach(System.out::println);
Arrays.stream(args.getSourceArgs()).forEach(System.out::println);
}
}

 

package com.runner.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Order(2)
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(" Start preloading data (CommandLineRunner)...");
Arrays.stream(args).forEach(System.out::println);
}
}

  result :

 Start preloading data (CommandLineRunner)...
--spring.profile.active=test
Start preloading data (ApplicationRunner)...
spring.profile.active
--spring.profile.active=test
RunnerApplication completion of enforcement 

copyright:author[Programmer light],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130752372546.html