Spingboot2.x—— devtools进行热部署

spring-boot-devtools模块包括对应用程序快速重启的支持,spring-boot-devtools只要类路径上的文件发生更改,使用的应用程序就会自动重新启动。在IDE中工作时,这可能是一个有用的功能,因为它为代码更改提供了非常快速的反馈循环。默认情况下,将监视类路径上指向文件夹的任何条目的更改。请注意,某些资源(例如静态资产和视图模板)不需要重新启动应用程序

该spring-boot-devtools模块可以包含在任何项目中,以提供其他开发时功能。要包括devtools支持,请将模块依赖项添加到您的构建中

在pom文件添加:

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

一、设置IDE内修改类文件、application.properties等配置文件自动重启

1、在Idea内启动springboot项目,再修改某个controllar类保存,发现服务并没有重启,这时候需要设置Idea中一些配置

当DevTools监视类路径资源时,触发重启的唯一方法是更新类路径。导致类路径更新的方式取决于所使用的IDE。在Eclipse中,保存修改后的文件将导致类路径被更新并触发重新启动。在IntelliJ IDEA中,构建项目(Build +→+ Build Project)具有相同的效果。

2、修改Idea中自动重启相关内容

Settting——>搜索Compiler——>Build Project Automatically

Help——>Find Action——>搜索Registry 回车——>勾选compiler.automake

 

即使开发的应用程序当前正在运行,也允许自动生成启动

3、重启服务,修改一个controllar保存,发现程序已经自动启动成功

二、禁用重启功能

1、配置文件下添加:spring.devtools.restart.enabled=false

进入该配置项,查看源码:

/**
 * Restart properties.
 */
public static class Restart {

   private static final String DEFAULT_RESTART_EXCLUDES = "META-INF/maven/**,"
         + "META-INF/resources/**,resources/**,static/**,public/**,templates/**,"
         + "**/*Test.class,**/*Tests.class,git.properties,META-INF/build-info.properties";

   /**
    * Whether to enable automatic restart.
    */
   private boolean enabled = true;

   /**
    * Patterns that should be excluded from triggering a full restart.
    */
   private String exclude = DEFAULT_RESTART_EXCLUDES;

   /**
    * Additional patterns that should be excluded from triggering a full restart.
    */
   private String additionalExclude;

   /**
    * Amount of time to wait between polling for classpath changes.
    */
   private Duration pollInterval = Duration.ofSeconds(1);

   /**
    * Amount of quiet time required without any classpath changes before a restart is
    * triggered.
    */
   private Duration quietPeriod = Duration.ofMillis(400);

   /**
    * Name of a specific file that, when changed, triggers the restart check. Must be
    * a simple name (without any path) of a file that appears on your classpath. If
    * not specified, any classpath file change triggers the restart.
    */
   private String triggerFile;

   /**
    * Additional paths to watch for changes.
    */
   private List<File> additionalPaths = new ArrayList<>();

   /**
    * Whether to log the condition evaluation delta upon restart.
    */
   private boolean logConditionEvaluationDelta = true;

   public boolean isEnabled() {
      return this.enabled;
   }

   public void setEnabled(boolean enabled) {
      this.enabled = enabled;
   }

   public String[] getAllExclude() {
      List<String> allExclude = new ArrayList<>();
      if (StringUtils.hasText(this.exclude)) {
         allExclude.addAll(StringUtils.commaDelimitedListToSet(this.exclude));
      }
      if (StringUtils.hasText(this.additionalExclude)) {
         allExclude.addAll(StringUtils.commaDelimitedListToSet(this.additionalExclude));
      }
      return StringUtils.toStringArray(allExclude);
   }

   public String getExclude() {
      return this.exclude;
   }

   public void setExclude(String exclude) {
      this.exclude = exclude;
   }

   public String getAdditionalExclude() {
      return this.additionalExclude;
   }

   public void setAdditionalExclude(String additionalExclude) {
      this.additionalExclude = additionalExclude;
   }

   public Duration getPollInterval() {
      return this.pollInterval;
   }

   public void setPollInterval(Duration pollInterval) {
      this.pollInterval = pollInterval;
   }

   public Duration getQuietPeriod() {
      return this.quietPeriod;
   }

   public void setQuietPeriod(Duration quietPeriod) {
      this.quietPeriod = quietPeriod;
   }

   public String getTriggerFile() {
      return this.triggerFile;
   }

   public void setTriggerFile(String triggerFile) {
      this.triggerFile = triggerFile;
   }

   public List<File> getAdditionalPaths() {
      return this.additionalPaths;
   }

   public void setAdditionalPaths(List<File> additionalPaths) {
      this.additionalPaths = additionalPaths;
   }

   public boolean isLogConditionEvaluationDelta() {
      return this.logConditionEvaluationDelta;
   }

   public void setLogConditionEvaluationDelta(boolean logConditionEvaluationDelta) {
      this.logConditionEvaluationDelta = logConditionEvaluationDelta;
   }

}

 

spring.devtools.restart.enabled 重新启动: 默认重启功能是true打开的

pring-boot-devtools模块包括一个嵌入式LiveReload服务器,该服务器可用于在更改资源时触发浏览器刷新

spring.devtools.restart.exclude =静态/ **,公共/ ** 重新设置排除项

默认静态资源路径下DEFAULT_RESTART_EXCLUDES不会自动重启,但是会重新加载,实际请求资源文件会生效

spring.devtools.restart.exclude=public/**,我修改static下面资源文件发现项目自动重启了,public也会生效,不会重启项目

 

参考:https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-devtools

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值