詳解Spring Boot中初始化資源的幾種方式
假設有這么一個需求,要求在項目啟動過程中,完成線程池的初始化,加密證書加載等功能,你會怎么做?如果沒想好答案,請接著往下看。今天介紹幾種在Spring Boot中進行資源初始化的方式,幫助大家解決和回答這個問題。
CommandLineRunner
- 定義初始化類 MyCommandLineRunner
- 實現 CommandLineRunner 接口,并實現它的 run() 方法,在該方法中編寫初始化邏輯
- 注冊成Bean,添加 @Component注解即可
示例代碼如下:
@Component public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("...init resources by implements CommandLineRunner"); } }
實現了 CommandLineRunner 接口的 Component 會在所有 Spring Beans 初始化完成之后, 在 SpringApplication.run() 執(zhí)行之前完成。下面通過加兩行打印來驗證我們的測試。
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { System.out.println("... start SpringApplication.run()"); SpringApplication.run(DemoApplication.class, args); System.out.println("... end SpringApplication.run()"); } }
控制臺打印結果如下。
... start SpringApplication.run()
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.11.RELEASE)
。。。。。。(此處省略一堆打印信息)
2018-05-02 17:01:19.700 INFO 21236 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
...init resources by implements CommandLineRunner
2018-05-02 17:01:19.708 INFO 21236 --- [ main] cn.mariojd.demo.DemoApplication : Started DemoApplication in 2.282 seconds (JVM running for 3.125)
... end SpringApplication.run()
ApplicationRunner
- 定義初始化類 MyApplicationRunner
- 實現 ApplicationRunner 接口,并實現它的 run() 方法,在該方法中編寫初始化邏輯
- 注冊成Bean,添加 @Component注解即可
示例代碼如下:
@Component public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments applicationArguments) throws Exception { System.out.println("...init resources by implements ApplicationRunner"); } }
可以看到,通過實現 ApplicationRunner 接口,和通過實現 CommandLineRunner 接口都可以完成項目的初始化操作,實現相同的效果。兩者之間唯一的區(qū)別是 run() 方法中自帶的形參不相同,在 CommandLineRunner 中只是簡單的String... args形參,而 ApplicationRunner 則是包含了 ApplicationArguments 對象,可以幫助獲得更豐富的項目信息。
ApplicationArguments
@Order
如果項目中既有實現了 ApplicationRunner 接口的初始化類,又有實現了 CommandLineRunner 接口的初始化類,那么會是哪一個先執(zhí)行呢?測試告訴我們,答案是實現了 ApplicationRunner 接口的初始化類先執(zhí)行,我想這點倒是不需要大家過分去關注為什么。但如果需要改變兩個初始化類之間的默認執(zhí)行順序,那么使用 @Order 注解就可以幫助我們解決這個問題。
@Order @Component @Order(1) public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("...init resources by implements CommandLineRunner"); } }
@Component @Order(2) public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments applicationArguments) throws Exception { System.out.println("...init resources by implements ApplicationRunner"); } }
最終,控制臺中打印如下。通過控制臺輸出我們發(fā)現, @Order 注解值越小,該初始化類也就越早執(zhí)行。
。。。。。。(此處省略一堆打印信息)
2018-05-02 17:27:31.450 INFO 28304 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
...init resources by implements CommandLineRunner
...init resources by implements ApplicationRunner
2018-05-02 17:27:31.453 INFO 28304 --- [ main] cn.mariojd.demo.DemoApplication : Started DemoApplication in 2.086 seconds (JVM running for 2.977)
@PostConstruct
使用 @PostConstruct 注解同樣可以幫助我們完成資源的初始化操作,前提是這些初始化操作不需要依賴于其它Spring beans的初始化工作。
@PostConstruct
可以看到 @PostConstruct 注解是用在方法上的,寫一個方法測試一下吧。
@PostConstruct public void postConstruct() { System.out.println("... PostConstruct"); }
啟動項目,控制臺中最終打印如下。
... start SpringApplication.run()
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.11.RELEASE)
。。。。。。(此處省略一堆打印信息)
... PostConstruct
。。。。。。(此處省略一堆打印信息)
2018-05-02 17:40:22.300 INFO 29796 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
...init resources by implements CommandLineRunner
...init resources by implements ApplicationRunner
2018-05-02 17:40:22.303 INFO 29796 --- [ main] cn.mariojd.demo.DemoApplication : Started DemoApplication in 2.387 seconds (JVM running for 3.267)
... end SpringApplication.run()
文末小結
綜上,使用 @PostConstruct 注解進行初始化操作的順序是最快的,前提是這些操作不能依賴于其它Bean的初始化完成。通過添加 @Order 注解,我們可以改變同層級之間不同Bean的加載順序。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SSH框架網上商城項目第3戰(zhàn)之使用EasyUI搭建后臺頁面框架
SSH框架網上商城項目第3戰(zhàn)之使用EasyUI搭建后臺頁面框架,討論兩種搭建方式:基于frameset和基于easyUI,感興趣的小伙伴們可以參考一下2016-05-05slf4j與jul、log4j1、log4j2、logback的集成原理
這篇文章主要介紹了slf4j與jul、log4j1、log4j2、logback的集成原理,以及通用日志框架與具體日志實現系統(tǒng)的機制機制介紹,包括依賴的jar包,jar沖突處理等2022-03-03Java線程之鎖對象Lock-同步問題更完美的處理方式代碼實例
這篇文章主要介紹了Java線程之鎖對象Lock-同步問題更完美的處理方式代碼實例,還是挺不錯的,這里分享給大家,需要的朋友可以參考。2017-11-11淺談緩沖字符流 BufferedReader BufferedWriter用法
這篇文章主要介紹了緩沖字符流 BufferedReader BufferedWriter的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07使用@ConfigurationProperties注解獲取為null的解決方法
在SpringBoot中,當想需要獲取到配置文件數據時,除了可以用 Spring 自帶的@Value注解外,SpringBoot還提供了一種更加方便的方式:@ConfigurationProperties,但我們在通過通過get方法去取值一直為null,本文介紹了使用@ConfigurationProperties注解獲取為null的解決方法2024-09-09