假設有這么一個需求,要求在項目啟動過程中,完成線程池的初始化,加密證書加載等功能,你會怎么做?如果沒想好答案,請接著往下看。今天介紹幾種在spring boot中進行資源初始化的方式,幫助大家解決和回答這個問題。
commandlinerunner
- 定義初始化類 mycommandlinerunner
- 實現 commandlinerunner 接口,并實現它的 run() 方法,在該方法中編寫初始化邏輯
- 注冊成bean,添加 @component注解即可
示例代碼如下:
1
2
3
4
5
6
7
8
9
|
@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() 執行之前完成。下面通過加兩行打印來驗證我們的測試。
1
2
3
4
5
6
7
8
9
10
|
@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注解即可
示例代碼如下:
1
2
3
4
5
6
7
8
9
|
@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 對象,可以幫助獲得更豐富的項目信息。
applicationarguments
@order
如果項目中既有實現了 applicationrunner 接口的初始化類,又有實現了 commandlinerunner 接口的初始化類,那么會是哪一個先執行呢?測試告訴我們,答案是實現了 applicationrunner 接口的初始化類先執行,我想這點倒是不需要大家過分去關注為什么。但如果需要改變兩個初始化類之間的默認執行順序,那么使用 @order 注解就可以幫助我們解決這個問題。
1
2
3
4
5
6
7
8
9
10
|
@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" ); } } |
1
2
3
4
5
6
7
8
9
10
|
@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 注解是用在方法上的,寫一個方法測試一下吧。
1
2
3
4
|
@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的加載順序。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_28804275/article/details/80891941