溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

springboot項目啟動時初始化資源如何解決

發(fā)布時間:2021-02-02 13:52:04 來源:億速云 閱讀:165 作者:Leah 欄目:開發(fā)技術

springboot項目啟動時初始化資源如何解決?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

運用案例測試它如何使用,在測試之前在啟動類加兩行打印提示,方便我們識別 CommandLineRunner 的執(zhí)行時機。

@SpringBootApplication
public class SpringbootRabbitmqApplication {

	public static void main(String[] args) {
    System.out.println("The service to start");
	  SpringApplication.run(SpringbootRabbitmqApplication.class, args);
    System.out.println("The service to started");
	}

}

接下來我們直接創(chuàng)建一個類繼承 CommandLineRunner ,并實現(xiàn)它的 run() 方法。

@Component
public class Runner implements CommandLineRunner {
  
  @Override
  public void run(String... args) throws Exception {
    System.out.println("The Runner start to initialize ...");
  }
  
}

啟動項目進行測試:

...
The service to start.

 .  ____     _      __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::    (v2.0.2.RELEASE)

...
2021-02-01 11:38:31.314 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8078 (http) with context path ''
2021-02-01 11:38:31.317 [main] INFO com.cn.SpringbootRabbitmqApplication - Started SpringbootRabbitmqApplication in 4.124 seconds (JVM running for 6.226)
The Runner start to initialize ...
The service to started

根據(jù)控制臺的打印信息我們可以看出 CommandLineRunner 中的方法會在 Spring Boot 容器加載之后執(zhí)行,執(zhí)行完成后項目啟動完成。

如果我們在啟動容器的時候需要初始化很多資源,并且初始化資源相互之間有序,那如何保證不同的 CommandLineRunner 的執(zhí)行順序呢?Spring Boot 也給出了解決方案。那就是使用 @Order 注解。

我們創(chuàng)建兩個 CommandLineRunner 的實現(xiàn)類來進行測試:

第一個實現(xiàn)類:

@Component
@Order(1)
public class OrderRunner1 implements CommandLineRunner {
  @Override
  public void run(String... args) throws Exception {
    System.out.println("The OrderRunner1 start to initialize ...");
  }
}

第二個實現(xiàn)類:

@Component
@Order(2)
public class OrderRunner2 implements CommandLineRunner {
  @Override
  public void run(String... args) throws Exception {
    System.out.println("The OrderRunner2 start to initialize ...");
  }
}

添加完成之后重新啟動,觀察執(zhí)行順序:

...
The service to start.
 .  ____     _      __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::    (v2.0.2.RELEASE)

...
2021-02-01 11:42:05.724 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8078 (http) with context path ''
2021-02-01 11:42:05.728 [main] INFO com.cn.SpringbootRabbitmqApplication - Started SpringbootRabbitmqApplication in 3.472 seconds (JVM running for 5.473)
The OrderRunner1 start to initialize ...
The OrderRunner2 start to initialize ...
The Runner start to initialize ...
The service to started

通過控制臺的輸出我們發(fā)現(xiàn),添加 @Order 注解的實現(xiàn)類最先執(zhí)行,并且@Order()里面的值越小啟動越早。

看完上述內(nèi)容,你們掌握springboot項目啟動時初始化資源如何解決的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI