版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hju22/article/details/87887998
热部署的原理是在发现代码有更改后会立即重启应用,但这个速度比手动停止后再启动要快。重启应用的时机发生在保存代码的时候。
一、实现热部署的步骤
1、开启自动编译
1、点击file -->点击settings -->展开 build,execution,deployment -->点击compiler -->勾选 build project automatically -->点击ok
2、Ctrl+Shift+A -->registry -->勾选compiler.automake.allow.when.app.running
3、重启idea
2、添加devtools依赖
1、在pom.xml中添加devtools依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
<scope>runtime</scope>
</dependency>
2、在pom.xml中添加配置fork,开启热部署
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork> <!-- 这个需要为 true 热部署才有效 -->
</configuration>
</plugin>
</plugins>
</build>
这样,热部署就实现了
二、关闭热部署的两种方式
1、在 application.properties设置:
spring.devtools.restart.enabled=false
2、设置环境变量
public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled","true");
SpringApplication.run(RestfulApplication.class, args);
}
三、测试热部署功能
1、修改类,保存 --> 应用会重启
2、修改配置文件,保存 --> 应用会重启
3、修改页面,保存 --> 应用会重启,页面会刷新