在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/

原文地址:https://www.cnblogs.com/asis/p/spring-boot-hot-swap.html