Preface
In our actual work, we always encounter such a requirement. When the project starts, we need to do some initialization operations, such as initializing the thread pool, loading the encryption certificate in advance, etc. Today I will introduce to you a Spring Boot tool, which is specifically designed to help you solve the initialization resource operation of project startup.
This artifact is CommandLineRunner. The Component of the CommandLineRunner interface will be executed before SpringApplication.run() after all SpringBeans are initialized, which is very suitable for some data initialization work at the beginning of application startup.
Next, we will use cases to test how it is used. Before testing, add two lines of print prompts to the startup class to facilitate us to identify the execution timing of CommandLineRunner.
@SpringBootApplicationpublic 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.
@Componentpublic class Runner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The Runner start to initialize..."); }}We print some parameters in the run() method to see how it executes. After completion, start the project for testing:
...The service to start.____ __ _ /// / ___'_ __ _(_)_ ____ _ / / / / ( ( )/___ | '_ | '_ | / / /` | / / / / / / / / / / / / / / | | | | | | | | | | | | | | || (_| | ) ) ) ) )' |____| .__|| |_| |_| |_| |_/__, | / / / / / =====================================____/=/_/_/ :: Spring Boot :: (v2.0.0.RELEASE)..2018-04-21 22:21:34.706 INFO 27016 --- [ main] osbwembedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''2018-04-21 22:21:34.710 INFO 27016 --- [ main] com.neo.CommandLineRunnerApplication : Started CommandLineRunnerApplication in 3.796 seconds (JVM running for 5.128)The Runner start to initialize ...The service has started.
Based on the printing information of the console, we can see that the method in CommandLineRunner will be executed after the Spring Boot container is loaded, and the project will be started after the execution is completed.
If we need to initialize many resources when starting the container and the initial resources are ordered with each other, how can we ensure the execution order of different CommandLineRunners? Spring Boot also provides a solution. That is to use @Order annotation.
We create two CommandLineRunner implementation classes to test:
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..."); }}After the addition is completed, restart and observe the execution order:
...The service to start. ____ _ __ _ //// ___'_ __ _(_)_ __ __ / / / / ( ( )/___ | '_ | '_| | / / / // ___)| | | | | | | | | | | | | | || (_| | ) ) ) ) )' |____| .__|| |_| |_| |_/__, | / / / / / ====================================___/=/_// :: Spring Boot :: (v2.0.0.RELEASE)...2018-04-21 22:21:34.706 INFO 27016 --- [ main] osbwembedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''2018-04-21 22:21:34.710 INFO 27016 --- [ main] com.neo.CommandLineRunnerApplication : Started CommandLineRunnerApplication in 3.796 seconds (JVM running for 5.128)The OrderRunner1 start to initialize ...The OrderRunner2 start to initialize ...The Runner start to initialize ...The service has started.
Through the console's output, we found that the implementation class that adds the @Order annotation is executed first, and the smaller the value in @Order(), the earlier it starts.
In practice, the same purpose can be achieved using ApplicationRunner, and there is little difference between the two.
Sample code: https://github.com/ityouknow/spring-cloud-examples (local download)
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.