最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Spring Boot中初始化資源的幾種方式

 更新時(shí)間:2018年08月06日 13:37:16   作者:happyJared  
這篇文章主要介紹了詳解Spring Boot中初始化資源的幾種方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

假設(shè)有這么一個(gè)需求,要求在項(xiàng)目啟動(dòng)過(guò)程中,完成線程池的初始化,加密證書(shū)加載等功能,你會(huì)怎么做?如果沒(méi)想好答案,請(qǐng)接著往下看。今天介紹幾種在Spring Boot中進(jìn)行資源初始化的方式,幫助大家解決和回答這個(gè)問(wèn)題。

CommandLineRunner

  1. 定義初始化類 MyCommandLineRunner
  2. 實(shí)現(xiàn) CommandLineRunner 接口,并實(shí)現(xiàn)它的 run() 方法,在該方法中編寫(xiě)初始化邏輯
  3. 注冊(cè)成Bean,添加 @Component注解即可

示例代碼如下:

@Component
public class MyCommandLineRunner implements CommandLineRunner {
 
  @Override
  public void run(String... args) throws Exception {
    System.out.println("...init resources by implements CommandLineRunner");
  }
  
}

實(shí)現(xiàn)了 CommandLineRunner 接口的 Component 會(huì)在所有 Spring Beans 初始化完成之后, 在 SpringApplication.run() 執(zhí)行之前完成。下面通過(guò)加兩行打印來(lái)驗(yàn)證我們的測(cè)試。

@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()");
  }
  
}

控制臺(tái)打印結(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

  1. 定義初始化類 MyApplicationRunner
  2. 實(shí)現(xiàn) ApplicationRunner 接口,并實(shí)現(xiàn)它的 run() 方法,在該方法中編寫(xiě)初始化邏輯
  3. 注冊(cè)成Bean,添加 @Component注解即可

示例代碼如下:

@Component
public class MyApplicationRunner implements ApplicationRunner {
 
  @Override
  public void run(ApplicationArguments applicationArguments) throws Exception {
    System.out.println("...init resources by implements ApplicationRunner");
  }
 
}

可以看到,通過(guò)實(shí)現(xiàn) ApplicationRunner 接口,和通過(guò)實(shí)現(xiàn) CommandLineRunner 接口都可以完成項(xiàng)目的初始化操作,實(shí)現(xiàn)相同的效果。兩者之間唯一的區(qū)別是 run() 方法中自帶的形參不相同,在 CommandLineRunner 中只是簡(jiǎn)單的String... args形參,而 ApplicationRunner 則是包含了 ApplicationArguments 對(duì)象,可以幫助獲得更豐富的項(xiàng)目信息。

ApplicationArguments

@Order

如果項(xiàng)目中既有實(shí)現(xiàn)了 ApplicationRunner 接口的初始化類,又有實(shí)現(xiàn)了 CommandLineRunner 接口的初始化類,那么會(huì)是哪一個(gè)先執(zhí)行呢?測(cè)試告訴我們,答案是實(shí)現(xiàn)了 ApplicationRunner 接口的初始化類先執(zhí)行,我想這點(diǎn)倒是不需要大家過(guò)分去關(guān)注為什么。但如果需要改變兩個(gè)初始化類之間的默認(rèn)執(zhí)行順序,那么使用 @Order 注解就可以幫助我們解決這個(gè)問(wèn)題。

@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");
  }
 
}

最終,控制臺(tái)中打印如下。通過(guò)控制臺(tái)輸出我們發(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 注解是用在方法上的,寫(xiě)一個(gè)方法測(cè)試一下吧。

  @PostConstruct
  public void postConstruct() {
    System.out.println("... PostConstruct");
  }

啟動(dòng)項(xiàng)目,控制臺(tái)中最終打印如下。

... 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()

文末小結(jié)

綜上,使用 @PostConstruct 注解進(jìn)行初始化操作的順序是最快的,前提是這些操作不能依賴于其它Bean的初始化完成。通過(guò)添加 @Order 注解,我們可以改變同層級(jí)之間不同Bean的加載順序。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

衡阳县| 新营市| 涪陵区| 天台县| 麻江县| 色达县| 合阳县| 永丰县| 即墨市| 松阳县| 大庆市| 阳曲县| 绿春县| 太仓市| 博客| 宣化县| 禄丰县| 平和县| 喀喇沁旗| 阳城县| 玉林市| 九江县| 乌拉特后旗| 陆河县| 河东区| 滕州市| 丁青县| 涟源市| 广西| 嘉定区| 秭归县| 内黄县| 崇左市| 杂多县| 陇西县| 宜丰县| 锡林浩特市| 财经| 前郭尔| 易门县| 达孜县|