idea2020 上實現 Springboot 熱部署


idea2020 上實現 Springboot 熱部署

個人實驗成功

一、導包

Developer Tools 官方文檔介紹[2.3.2.RELEAS]

Spring Boot includes an additional set of tools that can make the application development experience a little more pleasant. The spring-boot-devtools module can be included in any project to provide additional development-time features. To include devtools support, add the module dependency to your build, as shown in the following listings for Maven and Gradle:

機翻: Spring Boot包含一組額外的工具,可以使應用程序開發體驗更愉快一些。spring-boot-devtools 模塊可以包含在任何項目中,以提供額外的開發時特性。為了包含devtools的支持,添加模塊依賴到您的構建,如下面的Maven和Gradle清單所示:

Maven

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

Gradle

dependencies {
    developmentOnly("org.springframework.boot:spring-boot-devtools")
}

本人使用的maven. 有些教程還要再build里的plugin加fork,但我沒加也可以。

二、idea 設置

第一步、勾選 Build project automatically

具體步驟:打開頂部工具欄 File -> Settings -> 搜Compiler 然后勾選 Build project automatically

idea-setting

第二步、設置 Registry

具體步驟:同時按住 Ctrl + Shift + Alt + / 然后進入Registry ,勾選自動編譯並調整延時參數。
PS: 如果 Ctrl + Shift + Alt + / 沒有彈出, 可以使用 Ctrl + Shift + A 全局搜索 Registry

  • compiler.automake.allow.when.app.running -> 自動編譯
  • compile.document.save.trigger.delay -> 自動更新文件

作者:yizhiwazi:網上極少有人提到compile.document.save.trigger.delay 它主要是針對靜態文件如JS CSS的更新,將延遲時間減少后,直接按F5刷新頁面就能看到效果!

Registry

第三步、設置Configurations

具體步驟:頂部菜單 Run- >Edit Configurations->SpringBoot插件->目標項目->勾選熱更新。

Configurations

三、為什么要熱部署?

​                  Restart vs Reload
The restart technology provided by Spring Boot works by using two classloaders. Classes that do not change (for example, those from third-party jars) are loaded into a base classloader. Classes that you are actively developing are loaded into a restart classloader. When the application is restarted, the restart classloader is thrown away and a new one is created. This approach means that application restarts are typically much faster than “cold starts”, since the base classloader is already available and populated.

If you find that restarts are not quick enough for your applications or you encounter classloading issues, you could consider reloading technologies such as JRebel from ZeroTurnaround. These work by rewriting classes as they are loaded to make them more amenable to reloading.

上面就是官方文檔的原文,結合視頻,對於熱部署的優點個人理解是:

Spring Boot提供的重啟技術通過兩個類加載器來工作分別是 base classloaderrestart classloader 。 不變的內容(即引入的哪些包)是被 base classloaders 加載。而你的寫是被 restart classloader 加載。 熱部署就是只有 restart classloader 重新加載,而冷加載(cold starts)是兩個都重新加載,所以通常更快一些。

文檔中提到(機翻): 如果您發現重新啟動對您的應用程序來說不夠快,或者遇到類加載問題,您可以考慮重新加載 ZeroTurnaround 中的 JRebel 等技術。它們的工作原理是在類被加載時重寫類,使它們更易於重新加載。

JRebel 就是一個插件,這里貼個教程 JRebel插件使用詳解

四、排除某些資源

Certain resources do not necessarily need to trigger a restart when they are changed. For example, Thymeleaf templates can be edited in-place. By default, changing resources in /META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates does not trigger a restart but does trigger a live reload. If you want to customize these exclusions, you can use the spring.devtools.restart.exclude property. For example, to exclude only /static and /public you would set the following property:

spring.devtools.restart.exclude=static/**,public/**

就是默認/META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates不會觸發重新啟動(restart),但是會觸發 live reload , 默認排除的都是靜態資源,模板,前端頁面相關之類的。最后就是自定義排除一個示例。

參考 來源:

SpringBoot 在IDEA中實現熱部署(實用版)
官方文檔 2.3.2.RELEASE
B站視頻
b站最強新版Springboot教程 全程有廢話我直播吃紙


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM