1. 程式人生 > 其它 >【轉】關於springboot啟動時會執行兩次

【轉】關於springboot啟動時會執行兩次

文章轉自↓

https://blog.csdn.net/weixin_44280273/article/details/107307439

關於springboot啟動時會執行兩次
背景:今天在做專案的時候需要再啟動時進行自動操作,便想試試在啟動類中加入部分程式碼,便隨手加了一個輸出,執行了。然後發現輸出了兩次,對此上網進行相關資料查詢。
經過測試,偶然又發現具體是SpringApplication.run()前面的程式碼執行兩邊,而後面的程式碼只執行了一次

@SpringBootApplication
public class RetrogamesApplication {

    public static
void main(String[] args) { System.out.println("aaa"); SpringApplication.run(RetrogamesApplication.class, args); System.out.println("bbb"); } }

網上查詢後發現罪魁禍首是熱部署包,刪掉該依賴後沒有再出現上述問題

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

查詢過程也發現引入該依賴會有一些坑,如導致型別轉換異常等