IDEA2023及以上版本(高版本)设置SpringBoot热部署

一、手动启动热部署

步骤1:导入坐标

		<!--热部署坐标-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

步骤2:编写配置

spring:
  devtools:
    restart:
      enabled: true
      additional-paths: src/main/java 

步骤3:编写代码

步骤4:构建项目

在这里插入图片描述
对应的快捷键:Ctrl + F9

上述过程每次进行热部署都需要开发者手工操作,不管是点击按钮,还是快捷键都需要手工操作。如果能让程序自己执行热部署,就可以减少开发者操作,也就是自动进行热部署。详细步骤,看以下内容。

二、自动启动热部署

1、高版本配置

步骤1:导入坐标

		<!--热部署坐标-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

步骤2:编写配置

spring:
  devtools:
    restart:
      enabled: true
      additional-paths: src/main/java 

步骤3:配置项目自动编译

(1)File --> Settings… --> Build、Execution、Deployment -->Compile --> 勾选Build project automatically --> Apply --> OK
在这里插入图片描述
在这里插入图片描述
(2)File --> Settings… --> Advanced Settings --> 勾选Allow auto make to start even if developed application is currently running --> Apply --> OK
在这里插入图片描述
(3)同时按住 Ctrl + Alt + Shift + / ,出现弹窗,选择Registry…
在这里插入图片描述
(4)搜索:compiler.automake.postpone.when.idle.less.than,根据自己的需求修改value值(毫秒)
在这里插入图片描述

步骤4:修改代码,鼠标停留value的时间,自动编译

2、低版本配置

步骤1:导入坐标

		<!--热部署坐标-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

步骤2:编写配置

spring:
  devtools:
    restart:
      enabled: true
      additional-paths: src/main/java 

步骤3:配置项目自动编译

(1)File --> Settings… --> Build、Execution、Deployment -->Compile --> 勾选Build project automatically --> Apply --> OK
在这里插入图片描述
在这里插入图片描述
(2)同时按住 Ctrl + Alt + Shift + / ,出现弹窗,选择Registry…
在这里插入图片描述
(3)搜索:compiler.automake.allow.when.app.running,并勾选
在这里插入图片描述

步骤4:修改代码,鼠标停留value的时间,自动编译

三、热部署监控的文件范围配置

通过修改项目中的文件,可以发现并不是所有的文件修改都会激活热部署。原因在于在开发者工具中有一组配置,当满足了配置中的条件后,才会启动热部署,配置中默认不参与热部署的目录信息如下:

  1. /META-INF/maven
  2. /META-INF/resources
  3. /resources
  4. /static
  5. /public
  6. templates
    以上目录中的文件如果发生变化,是不参与热部署的。如果想修改配置,可以通过application.yml文件进行设定哪些文件不参与热部署操作。
spring:
 devtools:
   restart:
     # 设置不参与热部署的文件或文件夹
     exclude: static/**,public/**,resources/**

四、关闭热部署

热部署功能是一个典型的开发阶段使用的功能,到了线上环境运行程序时,这个功能就没有意义了。能否关闭热部署功能呢?
答案是肯定的。线上环境运行时是不可能使用热部署功能的,所以需要强制关闭此功能,通过配置可以关闭此功能。

spring:
 devtools:
   restart:
     enabled: false

如果当心配置文件层级过多导致相符覆盖最终引起配置失效,可以提高配置的层级,在更高层级中配置关闭热部署。例如在启动容器前通过系统属性设置关闭热部署功能。

@SpringBootApplication
public class Springboot10DevtoolsApplication {
    public static void main(String[] args) {
        //关闭热部署
        System.setProperty("spring.devtools.restart.enabled","false");
        SpringApplication.run(Springboot10DevtoolsApplication.class, args);
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值