spring boot初始化資源的幾種方式
轉自:https://www.jb51.net/article/145143.htm
實現 CommandLineRunner 介面,並實現它的 run() 方法,在該方法中編寫初始化邏輯
@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() 執行之前完成。下面通過加兩行列印來驗證我們的測試。
@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 介面,並實現它的 run() 方法,在該方法中編寫初始化邏輯
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
System.out.println("...init resources by implements ApplicationRunner");
}
}
可以看到,通過實現 ApplicationRunner
介面,和通過實現 CommandLineRunner
介面都可以完成專案的初始化操作,實現相同的效果。兩者之間唯一的區別
是 run() 方法中自帶的形參不相同,在 CommandLineRunner 中只是簡單的String… args形參,而 ApplicationRunner 則是包含了 ApplicationArguments 物件,可以幫助獲得更豐富的專案資訊。
優先順序 @Order
如果專案中既有實現了 ApplicationRunner 介面的初始化類,又有實現了 CommandLineRunner 介面的初始化類,那麼會是哪一個先執行呢?測試告訴我們,答案是實現了 ApplicationRunner 介面的初始化類先執行,我想這點倒是不需要大家過分去關注為什麼。但如果需要改變兩個初始化類之間的預設執行順序,那麼使用 @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");
}
}
最終,控制檯中列印如下。通過控制檯輸出我們發現, @Order 註解值越小
,該初始化類也就越早執行。
。。。。。。(此處省略一堆列印資訊)
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的載入順序。