Spring Boot 如何解決項目啟動時初始化資源

在我們實際工作中,總會遇到這樣需求,在項目啟動的時候需要做一些初始化的操作,比如初始化線程池,提前加載好加密證書等。今天就給大家介紹一個 Spring Boot 神器,專門幫助大家解決項目啟動初始化資源操作。

這個神器就是 CommandLineRunner, CommandLineRunner 接口的 Component 會在所有 SpringBeans都初始化之后, SpringApplication.run()之前執(zhí)行,非常適合在應用程序啟動之初進行一些數(shù)據(jù)初始化的工作。

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

@SpringBootApplication

public 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." ); }}接下來我們直接創(chuàng)建一個類繼承 CommandLineRunner ,并實現(xiàn)它的 run() 方法。

@Component

public class Runner implements CommandLineRunner {

@Override

public void run(String... args) throwsException{

System.out.println("The Runner start to initialize ...");

}

}

我們在 run() 方法中打印了一些參數(shù)來看出它的執(zhí)行時機。完成之后啟動項目進行測試:

...

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.

根據(jù)控制臺的打印信息我們可以看出 CommandLineRunner 中的方法會在 Spring Boot 容器加載之后執(zhí)行,執(zhí)行完成后項目啟動完成。

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

我們創(chuàng)建兩個 CommandLineRunner 的實現(xiàn)類來進行測試:

第一個實現(xiàn)類:

@Component

@Order(1)

public class OrderRunner1 implements CommandLineRunner {

@Override

publicvoid run(String... args) throwsException {

System.out.println("The OrderRunner1 start to initialize ...");

}

}

第二個實現(xiàn)類:

@Component

@Order(2)

public class OrderRunner2 implements CommandLineRunner {

@Override

public void run(String... args) throwsException {

System.out.println("The OrderRunner2 start to initialize ...");

}

}

添加完成之后重新啟動,觀察執(zhí)行順序:

...

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.

通過控制臺的輸出我們發(fā)現(xiàn),添加 @Order 注解的實現(xiàn)類最先執(zhí)行,并且 @Order()里面的值越小啟動越早。

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

在此我向大家推薦一個架構學習交流群。交流學習群號:938837867 里面會分享一些資深架構師錄制的視頻錄像:有Spring,MyBatis,Netty源碼分析,高并發(fā)、高性能、分布式、微服務架構的原理,JVM性能優(yōu)化、分布式架構等這些成為架構師必備的知識體系。還能領取免費的學習資源,目前受益良多

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容