idea2020上实现Springboot热部署
idea2020 上实现 Springboot 热部署
个⼈实验成功
⼀、导包
官⽅⽂档介绍[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>
spring boot依赖注入原理<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.igger.delay -> ⾃动更新⽂件
作者::⽹上极少有⼈提到compile.igger.delay 它主要是针对静态⽂件如JS CSS的更新,将延迟时间减少后,直接按F5刷新页⾯就能看到效果!
具体步骤:顶部菜单 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 就是⼀个插件,这⾥贴个教程
四、排除某些资源
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 . If you want to customize these exclusions, you can use the lude property. For example, to exclude only /static and /public you would set the following property:
lude=static/**,public/**
就是默认/META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates不会触发重新启动(restart),但是会触发 , 默认排除的都是静态资源,模板,前端页⾯相关之类的。最后就是⾃定义排除⼀个⽰例。
参考来源:
B站视频
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论