今天就跟大家聊聊有關(guān)Spring Boot中如何初始化資源,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都創(chuàng)新互聯(lián)成立10年來,這條路我們正越走越好,積累了技術(shù)與客戶資源,形成了良好的口碑。為客戶提供成都網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)站策劃、網(wǎng)頁設(shè)計、申請域名、網(wǎng)絡(luò)營銷、VI設(shè)計、網(wǎng)站改版、漏洞修補等服務(wù)。網(wǎng)站是否美觀、功能強大、用戶體驗好、性價比高、打開快等等,這些對于網(wǎng)站建設(shè)都非常重要,成都創(chuàng)新互聯(lián)通過對建站技術(shù)性的掌握、對創(chuàng)意設(shè)計的研究為客戶提供一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進步。
CommandLineRunner
定義初始化類 MyCommandLineRunner
實現(xiàn) CommandLineRunner 接口,并實現(xiàn)它的 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"); } }
實現(xiàn)了 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()"); } }
控制臺打印結(jié)果如下。
... 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
實現(xiàn) ApplicationRunner 接口,并實現(xiàn)它的 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"); } }
可以看到,通過實現(xiàn) ApplicationRunner 接口,和通過實現(xiàn) CommandLineRunner 接口都可以完成項目的初始化操作,實現(xiàn)相同的效果。兩者之間唯一的區(qū)別是 run() 方法中自帶的形參不相同,在 CommandLineRunner 中只是簡單的String... args形參,而 ApplicationRunner 則是包含了 ApplicationArguments 對象,可以幫助獲得更豐富的項目信息。
ApplicationArguments
@Order
如果項目中既有實現(xiàn)了 ApplicationRunner 接口的初始化類,又有實現(xiàn)了 CommandLineRunner 接口的初始化類,那么會是哪一個先執(zhí)行呢?測試告訴我們,答案是實現(xiàn)了 ApplicationRunner 接口的初始化類先執(zhí)行,我想這點倒是不需要大家過分去關(guān)注為什么。但如果需要改變兩個初始化類之間的默認(rèn)執(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ā)現(xiàn), @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()
看完上述內(nèi)容,你們對Spring Boot中如何初始化資源有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。