Spring Boot - devtools 热部署

spring-boot-devtools是Spring Boot提供的一组开发工具,它旨在提高开发体验。这些工具包括应用程序的自动重新启动、自动刷新和远程调试等功能。下面是将spring-boot-devtools整合到Spring Boot应用程序中的步骤:

0、启用"Build project automatically"(自动构建项目)选项

在这里插入图片描述

1、添加必要的依赖

 		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <!--确保将<optional>true</optional>加入Maven依赖,这样spring-boot-devtools只在开发环境中生效。-->
            <optional>true</optional>
            <!--在运行时有效-->
            <scope>runtime</scope>
        </dependency>

2、在application.yml添加一些 DevTools 配置

# Spring Boot DevTools 配置
spring:
  # DevTools 配置
  devtools:
    # 自动重启配置
    restart:
      # 启用自动重启
      enabled: true
      # 轮询间隔,检测文件变化的时间间隔
      poll-interval: 2s
      # 安静期间,避免重复触发的时间间隔
      quiet-period: 1s

3、来个controller简单示范

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class MyController {

    // http://localhost:8080/hello
    @GetMapping("/hello")
    public String hello() {
        return "Hello, Spring Boot 111!";
    }
}

4、修改启动配置: 勾选失去焦点时执行更新

在这里插入图片描述

5、启动日志里面会有这么一行

 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable

访问接口:
在这里插入图片描述
修改一下,再次访问:
在这里插入图片描述
如果刚刚的日志清空了,可见控制台第一行:

Restarting due to 1 class path change (0 additions, 0 deletions, 1 modification)

0添加 0删除 1修改

猜你喜欢

转载自blog.csdn.net/qq_43116031/article/details/134471867