springboot uses CommandLineRunner to solve the operation of initializing resources when the project starts

  • 2021-08-12 02:40:11
  • OfStack

Foreword:

In our actual work, we always encounter such requirements. When the project starts, we need to do some initialization operations, such as initializing the thread pool and loading the encryption certificate in advance.

Today, I will introduce you to an Spring Boot artifact, which is specially designed to help you solve the initialization resource operation of project startup.

This artifact is CommandLineRunner , CommandLineRunner Interface's Component Will be at all Spring Beans After all are initialized, SpringApplication.run() Before, it is very suitable for doing some data initialization at the beginning of application startup.

Text:

Next, we will use a case to test how to use it. Before the test, we will add two lines of printing prompts to the startup class, which is convenient for us to identify CommandLineRunner Timing of execution.


@SpringBootApplication
public class SpringbootRabbitmqApplication {

	public static void main(String[] args) {
    System.out.println("The service to start");
	  SpringApplication.run(SpringbootRabbitmqApplication.class, args);
    System.out.println("The service to started");
	}

}

Next, we directly create a class inheritance CommandLineRunner And implement its run() Method.


@Component
public class Runner implements CommandLineRunner {
  
  @Override
  public void run(String... args) throws Exception {
    System.out.println("The Runner start to initialize ...");
  }
  
}

Start the project for testing:


...
The service to start.

 .  ____     _      __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::    (v2.0.2.RELEASE)

...
2021-02-01 11:38:31.314 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8078 (http) with context path ''
2021-02-01 11:38:31.317 [main] INFO com.cn.SpringbootRabbitmqApplication - Started SpringbootRabbitmqApplication in 4.124 seconds (JVM running for 6.226)
The Runner start to initialize ...
The service to started

According to the printed information on the console, we can see that CommandLineRunner The method in is executed after the Spring Boot container is loaded, and the project startup is completed when the execution is complete.

If we need to initialize many resources when starting the container, and the initialized resources are orderly with each other, how can we ensure that they are different CommandLineRunner What about the execution order of? Spring Boot also gives a solution. That is to use the @ Order annotation.

We create two CommandLineRunner To test the implementation class of:

The first implementation class:


@Component
@Order(1)
public class OrderRunner1 implements CommandLineRunner {
  @Override
  public void run(String... args) throws Exception {
    System.out.println("The OrderRunner1 start to initialize ...");
  }
}

The second implementation class:


@Component
@Order(2)
public class OrderRunner2 implements CommandLineRunner {
  @Override
  public void run(String... args) throws Exception {
    System.out.println("The OrderRunner2 start to initialize ...");
  }
}

Restart after adding and observe the execution sequence:


...
The service to start.
 .  ____     _      __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::    (v2.0.2.RELEASE)

...
2021-02-01 11:42:05.724 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8078 (http) with context path ''
2021-02-01 11:42:05.728 [main] INFO com.cn.SpringbootRabbitmqApplication - Started SpringbootRabbitmqApplication in 3.472 seconds (JVM running for 5.473)
The OrderRunner1 start to initialize ...
The OrderRunner2 start to initialize ...
The Runner start to initialize ...
The service to started

From the output of the console, we found that adding @Order The implementation class of the annotation is executed first, and @Order() The smaller the value inside, the earlier it starts.

In practice, use the ApplicationRunner You can also achieve the same goal, and there is little difference between them.

The above is springboot using CommandLineRunner to solve the project start-up initialization of resources in detail, more about springboot solve the project start-up initialization of resources, please pay attention to other related articles on this site!


Related articles: