在IDEA下使用Spring Boot的熱加載(Hotswap)


你是否遇到過這樣的困擾:

當你寫完一段代碼后,要看到效果,必須點擊IDEA的停止按鈕,然后再次重啟啟動項目,你是否覺得這樣很煩呢?

如果你覺得很煩,本文就是用來解決你的問題的。

所謂熱加載,就是讓我們在寫完一段代碼后,不必重啟容器,刷新瀏覽器就能快速看到結果。

在IDEA中需要三個步驟來達到效果:

在build.gradle中加入devtools的依賴

compile("org.springframework.boot:spring-boot-devtools")

在application.properties中禁用模板引擎緩存

使用 FreeMarker

spring.freemarker.cache=false

參見: https://github.com/spring-projects/spring-boot/tree/v1.5.4.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java

使用 Thymeleaf

spring.thymeleaf.cache=false

參見: https://github.com/spring-projects/spring-boot/tree/v1.5.4.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java

使用 Groovy

spring.groovy.template.cache=false

參見: https://github.com/spring-projects/spring-boot/tree/v1.5.4.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java

修改IEDA設置

打開 Settings --> Build-Execution-Deployment --> Compiler,將 Build project automatically.勾上。

點擊 Help --> Find Action..,或使用快捷鍵 Ctrl+Shift+A來打開 Registry...,將 其中的compiler.automake.allow.when.app.running勾上。

全部設置完畢,重啟一下IDEA。

現在你就不必每次都手動的去點停止和啟動了。

參見例子:https://github.com/syler/heroku-lab/tree/hot_swap

本文連接:
http://www.cnblogs.com/asis/p/spring-boot-hot-swap.html

https://1few.com/spring-boot-hot-swap/


免責聲明!

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



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