SpringBoot开发模式自动重启热部署spring-boot-devtools

有几篇不错的文章

Triggering a restart

As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. The way in which you cause the classpath to be updated depends on the IDE that you are using:

  • In Eclipse, saving a modified file causes the classpath to be updated and triggers a restart.

  • In IntelliJ IDEA, building the project (Build +→+ Build Project) has the same effect.

简而言之就是,不同IDE触发重启方式不一样

  • Eclipse保存文件既可以触发重启
  • IntelliJ IDEA 需要重新构建项目

热部署

1、页面热部署
application.properties文件中配置

spring.thymeleaf.cache=false

2、类文件热部署

spring-boot-devtools

1、添加依赖

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
     <optional>true</optional>
 </dependency>

实际使用发现,不用配置application.yml也可以实现自动重启

配置编译插件

<build>
   <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!-- 如果没有该项配置,devtools不会起作用,即应用不会restart -->
                <fork>true</fork>
                <addResources>true</addResources>
            </configuration>
        </plugin>
    </plugins>
</build>

2、修改配置application.yml

spring:
  devtools:
    restart:
      enabled: true

3、将项目设置为自动加载

1、打开设置 勾选 Build project automatically
在这里插入图片描述
2、打开 Maintenance 选择 1.Register

快捷键 (一起按下四个按键)

windows: Ctrl + Shift +Alt + /

Mac : command + shift + option + /
在这里插入图片描述
3、勾选 compiler.automake.allow.when.app.running
在这里插入图片描述
IDEA过一会就会响应变化重启服务,如果想要快速触发,就重新编译

这里分享一份技巧:

保存文件的快捷键是 command + S

设置自动构建后需要等待一会才进行自动构建,这样明显不能等。
可以设置构建文件的快捷键:control + S,此时可以不设置自动构建,修改文件后手动进行构建。

Live Reload

Chrome插件 Live Reload 支持前端页面开发修改文件后自动刷新页面

注意:html、css、js文件同样需要build 到classpath路径下

总结:Spring Boot Live Reload

一、后端

使用依赖, 无需设置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <!--当前这个项目被继承之后,这个不向下传递-->
    <optional>true</optional>
</dependency>

java 代码的修改重启服务

二、前端

配合Chrome浏览器插件 Live Reload,无需设置

前端代码html、js、css文件的修改浏览器自动刷新

参考
SpringBoot热部署自动重启 spring-boot-devtools
【Spring Boot】Spring Boot在IDEA中使用devtools热部署配置不生效的解决办法

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值