Spring Boot 2.0 (vii): Spring boot How to resolve the initialization of a resource when a project starts

Source: Internet
Author: User

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.

@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 create a class inheritance directly CommandLineRunner and implement its run() methods.

@Componentpublic class Runner implements CommandLineRunner {    @Override    public void run(String... args) 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)public class OrderRunner1 implements CommandLineRunner {    @Override    public void run(String... args) throws Exception {        System.out.println("The OrderRunner1 start to initialize ...");    }}

A 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 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.