SpringBoot 如何解決項目啓動時初始化資源 前言: 正文:

前言:

在我們實際工作中,總會遇到這樣需求,在項目啓動的時候需要做一些初始化的操作,比如初始化線程池,提前加載好加密證書等。

今天就給大家介紹一個 Spring Boot 神器,專門幫助大家解決項目啓動初始化資源操作。

這個神器就是 CommandLineRunner,CommandLineRunner 接口的 Component 會在所有 Spring Beans 都初始化之後,SpringApplication.run() 之前執行,非常適合在應用程序啓動之初進行一些數據初始化的工作。

正文:

接下來我們就運用案例測試它如何使用,在測試之前在啓動類加兩行打印提示,方便我們識別 CommandLineRunner 的執行時機。

@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");
    }

}

接下來我們直接創建一個類繼承 CommandLineRunner ,並實現它的 run() 方法。

@Component
public class Runner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("The Runner start to initialize ...");
    }

}

啓動項目進行測試:

...
The service to start.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.2.RELEASE)

...
2021-02-01 11:38:31.314 [main] INFO  o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8078 (http) with context path ''
2021-02-01 11:38:31.317 [main] INFO  com.cn.SpringbootRabbitmqApplication - Started SpringbootRabbitmqApplication in 4.124 seconds (JVM running for 6.226)
The Runner start to initialize ...
The service to started

根據控制檯的打印信息我們可以看出 CommandLineRunner 中的方法會在 Spring Boot 容器加載之後執行,執行完成後項目啓動完成。

如果我們在啓動容器的時候需要初始化很多資源,並且初始化資源相互之間有序,那如何保證不同的 CommandLineRunner 的執行順序呢?Spring Boot 也給出瞭解決方案。那就是使用 @Order 註解。

我們創建兩個 CommandLineRunner 的實現類來進行測試:

第一個實現類:

@Component
@Order(1)
public class OrderRunner1 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("The OrderRunner1 start to initialize ...");
    }
}

第二個實現類:

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

添加完成之後重新啓動,觀察執行順序:

...
The service to start.
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.2.RELEASE)

...
2021-02-01 11:42:05.724 [main] INFO  o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8078 (http) with context path ''
2021-02-01 11:42:05.728 [main] INFO  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 started

通過控制檯的輸出我們發現,添加 @Order 註解的實現類最先執行,並且@Order()裏面的值越小啓動越早。

在實踐中,使用ApplicationRunner也可以達到相同的目的,兩着差別不大。

作者:maomaoJava
鏈接:https://juejin.cn/post/6924146777675268109

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章