在咱們實際工做中,總會遇到這樣需求,在項目啓動的時候須要作一些初始化的操做,好比初始化線程池,提早加載好加密證書等。今天就給你們介紹一個 Spring Boot 神器,專門幫助你們解決項目啓動初始化資源操做。tomcat
這個神器就是 CommandLineRunner,CommandLineRunner 接口的 Component 會在全部 Spring Beans 都初始化以後,SpringApplication.run() 以前執行,很是適合在應用程序啓動之初進行一些數據初始化的工做。ide
接下來咱們就運用案例測試它如何使用,在測試以前在啓動類加兩行打印提示,方便咱們識別 CommandLineRunner 的執行時機。測試
@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."); } }
接下來咱們直接建立一個類繼承 CommandLineRunner ,並實現它的 run() 方法。加密
@Component public class Runner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The Runner start to initialize ..."); } }
咱們在 run() 方法中打印了一些參數來看出它的執行時機。完成以後啓動項目進行測試:.net
... 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.
根據控制檯的打印信息咱們能夠看出 CommandLineRunner 中的方法會在 Spring Boot 容器加載以後執行,執行完成後項目啓動完成。線程
若是咱們在啓動容器的時候須要初始化不少資源,而且初始化資源相互之間有序,那如何保證不一樣的 CommandLineRunner 的執行順序呢?Spring Boot 也給出瞭解決方案。那就是使用 @Order 註解。code
咱們建立兩個 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.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.
經過控制檯的輸出咱們發現,添加 @Order 註解的實現類最早執行,而且@Order()裏面的值越小啓動越早。
在實踐中,使用ApplicationRunner也能夠達到相同的目的,兩着差異不大。看來使用 Spring Boot 解決初始化資源的問題很是簡單。