Art is long, life is long

Preface:

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.

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

Body:

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 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"); }}Copy the code

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) throws Exception {
        System.out.println("The Runner start to initialize ..."); }}Copy the code

Start the project for testing:

. The service to start. . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | | | (_ | |))))There comes' | |. __ _ - | | | _ _ - | | | _ \ __, | / / / / = = = = = = = = = | _ | = = = = = = = = = = = = = = = | ___ / / _ / _ / _ / : : Spring the Boot: : (v2.0.2. RELEASE)... The 2021-02-01 11:38:31. 314. [the main] INFO O.S.B oot. Web. Embedded. Tomcat. TomcatWebServer - tomcat started on the port (s) : 8078 (http) with context path ''the 11:38:31 2021-02-01. [the main] INFO 317 com. Cn. SpringbootRabbitmqApplication - Started SpringbootRabbitmqApplication in 4.124 seconds (JVM running for 6.226) The Runner start to initialize... The service to startedCopy 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
    public void run(String... args) throws Exception {
        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) throws Exception {
        System.out.println("The OrderRunner2 start to initialize ..."); }}Copy the code

Restart after adding and observe the order of execution:

. The service to start. . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | | | (_ | |))))There comes' | |. __ _ - | | | _ _ - | | | _ \ __, | / / / / = = = = = = = = = | _ | = = = = = = = = = = = = = = = | ___ / / _ / _ / _ / : : Spring the Boot: : (v2.0.2. RELEASE)... The 2021-02-01 11:42:05. 724. [the main] INFO O.S.B oot. Web. Embedded. Tomcat. TomcatWebServer - tomcat started on the port (s) : 8078 (http) with context path ''the 11:42:05 2021-02-01. [the main] INFO 728 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 startedCopy 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.