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
第二步、设置 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刷新页面就能看到效果!
第三步、设置Configurations
具体步骤:顶部菜单 Run- >Edit Configurations->SpringBoot插件->目标项目->勾选热更新。
三、为什么要热部署?
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 classloader 和 restart 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教程 全程有废话我直播吃纸