In our actual work, there are always such requirements that we need to do some initialization operations at the start of the project, such as initializing the thread pool, loading the encryption certificate in advance, and so on. Today we will introduce a Spring Boot wizard, specifically to help you solve the project startup initialization resource operation.


CommandLineRunner. The Component of the CommandLineRunner interface executes after all SpringBeans have been initialized and before SpringApplication.run(). Great for doing some data initialization at the start of an application.

We will then use a case study to test how it works, adding two lines of print prompts to the startup class before testing to identify when CommandLineRunner should be executed.

@SpringBootApplication
public class CommandLineRunnerApplication {
 public static void main(String[] args) {
 System.out.println("The service to start.");
 SpringApplication.run(CommandLineRunnerApplication.class, args);
 System . out .println( "The service has started."); }} Next we directly create a class that inherits CommandLineRunner and implements its Run () method. @Component public class Runner implements CommandLineRunner { @Override public void run(String... args) throwsException{ System.out.println("The Runner start to initialize ..."); }}Copy the code

We print some arguments in the run() method to see when it will be executed. After completion, start the project for testing:

. The service to start. :: Spring Boot :: (v2.0.0.release)... The 22:21:34 2018-04-21. 27016-706 the INFO [main] O.S.B.W.E mbedded. Tomcat. TomcatWebServer: tomcat started on the port (s) : 8080 (http) with context path' 'The 2018-04-21 22:21:34. 27016-710 the INFO [the main] com.neo.Com mandLineRunnerApplication: Started CommandLineRunnerApplicationin3.796 seconds (JVM is runningfor5.128) The Runner start to initialize... The service has started.Copy the code

As you can see from the console print, the methods in CommandLineRunner are executed after the Spring Boot container is loaded and the project is started.

If we need to initialize many resources when starting a container, and the resources are initialized in an orderly way, how do we ensure that the different commandLinerunners are executed in order? Spring Boot also offers a solution. That’s using the @order annotation.

We create two implementation classes for CommandLineRunner to test:

The first implementation class:

@Component
@Order(1)
public class OrderRunner1 implements CommandLineRunner {
 @Override
 publicvoid run(String... args) throwsException {
 System.out.println("The OrderRunner1 start to initialize ..."); }}Copy the code

The second implementation class:

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

Restart after adding and observe the order of execution:

. The service to start. :: Spring Boot :: (v2.0.0.release)... The 22:21:34 2018-04-21. 27016-706 the INFO [main] O.S.B.W.E mbedded. Tomcat. TomcatWebServer: tomcat started on the port (s) : 8080 (http) with context path' 'The 2018-04-21 22:21:34. 27016-710 the INFO [the main] com.neo.Com mandLineRunnerApplication: Started CommandLineRunnerApplicationin3.796 seconds (JVM is runningforThe OrderRunner1 start to initialize... The OrderRunner2 start to initialize ... The Runner start to initialize ... The service has started.Copy the code

From the console output, we see that the implementation class with the @Order annotation executes first, and the smaller the value inside @order (), the earlier it starts.

In practice, the same goal can be achieved using ApplicationRunner without much difference.

Here I recommend an architecture learning exchange group. Exchange learning group number: 938837867 inside will share some senior architects recorded video video: Spring, MyBatis, Netty source code analysis, high concurrency, high performance, distributed, microservice architecture principle, JVM performance optimization, distributed architecture and so on these become architects necessary knowledge system. I can also get free learning resources, which I benefit a lot from now