In our actual work, there is always the need to do some initialization when the project starts, such as initializing the thread pool, loading the encryption certificate in advance. Today we will introduce a Spring boot artifact, specifically to help you solve the project start initialization resource operation.
The artifact is CommandLineRunner
that the CommandLineRunner
interface Component
will be Spring Beans
executed before all is initialized, SpringApplication.run()
making it ideal for some data initialization at the beginning of the application.
We then use the case to test how it is used, and two lines of print prompts in the startup class before the test to facilitate our identification CommandLineRunner
of the timing of execution.
@SpringBootApplicationpublicclass CommandLineRunnerApplication { publicstaticvoidmain(String[] args) { System.out.println("The service to start."); SpringApplication.run(CommandLineRunnerApplication.class, args); System.out.println("The service has started."); }}
Next we create a class inheritance directly CommandLineRunner
and implement its run()
methods.
@Componentpublicclassimplements CommandLineRunner { @Override publicvoidrun(String... throws Exception { System.out.println("The Runner start to initialize ..."); }}
We run()
printed some parameters in the method to see the timing of its execution. When you are finished, start the project to test:
...The service to start. . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.0.RELEASE)...2018-04-21 22:21:34.706 INFO 27016 --- [ main] o.s.b.w.embedded.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.
Depending on the printing information in the console, we can see that CommandLineRunner
the method is executed after the Spring boot container is loaded, and the project starts after execution is complete.
If we need to initialize a lot of resources when we start the container and initialize the resources in order, how do we guarantee the different CommandLineRunner
order of execution? Spring Boot also gives a solution. That's the use of @Order
annotations.
We create two CommandLineRunner
implementation classes to test:
The first implementation class:
@Component@Order(1)publicclassimplements CommandLineRunner { @Override publicvoidrun(String... throws Exception { System.out.println("The OrderRunner1 start to initialize ..."); }}
A second implementation class:
@Component@Order(2)publicclassimplements CommandLineRunner { @Override publicvoidrun(String... throws Exception { System.out.println("The OrderRunner2 start to initialize ..."); }}
After the addition is complete, reboot to observe the order of execution:
...The service to start. . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.0.RELEASE)...2018-04-21 22:21:34.706 INFO 27016 --- [ main] o.s.b.w.embedded.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.
With the output of the console we find that the implementation class that added the @Order
annotation is executed first, and @Order()
the smaller the value inside, the sooner it starts.
In practice, the use ApplicationRunner
can also achieve the same purpose, two differences are not big. It seems simple to use Spring Boot to solve the problem of initializing resources.
Sample Code-github
Sample code-Cloud Code
Spring Boot 2.0 (vii): Spring boot How to resolve the initialization of a resource when a project starts