有幾篇不錯的文章 https://springframework.guru/spring-boot-developer-tools/ https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.devtools
Triggering a restart
As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. The way in which you cause the classpath to be updated depends on the IDE that you are using:
- In Eclipse, saving a modified file causes the classpath to be updated and triggers a restart.
- In IntelliJ IDEA, building the project (Build +→+ Build Project) has the same effect.
簡而言之就是,不同IDE觸發重新開機方式不一樣
- Eclipse儲存檔案既可以觸發重新開機
- IntelliJ IDEA 需要重新建構項目
熱部署
1、頁面熱部署
application.properties檔案中配置
spring.thymeleaf.cache=false
2、類檔案熱部署
spring-boot-devtools
1、添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
實際使用發現,不用配置
application.yml
也可以實作自動重新開機
2、修改配置application.yml
spring:
devtools:
restart:
enabled: true
3、将項目設定為自動加載
1、打開設定 勾選 Build project automatically
2、打開 Maintenance 選擇 1.Register
快捷鍵 (一起按下四個按鍵)
windows: Ctrl + Shift +Alt + /
Mac : command + shift + option + /
3、勾選 compiler.automake.allow.when.app.running
IDEA過一會就會響應變化重新開機服務,如果想要快速觸發,就重新編譯
這裡分享一份技巧:
儲存檔案的快捷鍵是 command + S
設定自動建構後需要等待一會才進行自動建構,這樣明顯不能等。
可以設定建構檔案的快捷鍵:control + S,此時可以不設定自動建構,修改檔案後手動進行建構。
Live Reload
Chrome插件 Live Reload 支援前端頁面開發修改檔案後自動重新整理頁面
注意:html、css、js檔案同樣需要
build
到classpath路徑下
總結:Spring Boot Live Reload
一、後端
使用依賴, 無需設定
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<!--目前這個項目被繼承之後,這個不向下傳遞-->
<optional>true</optional>
</dependency>
java 代碼的修改重新開機服務
二、前端
配合Chrome浏覽器插件 Live Reload,無需設定
前端代碼html、js、css檔案的修改浏覽器自動重新整理
參考
SpringBoot熱部署自動重新開機 spring-boot-devtools