步遥情感网
您的当前位置:首页springboot项目如何实现热更新

springboot项目如何实现热更新

来源:步遥情感网

需要添加 spring-boot-devtools 依赖并进行相关配置。

1、首先在 pom.xml 中添加依赖:

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

2、修改 application.yml 添加热更新配置:

spring:
  # 已有的配置保持不变...
  devtools:
    restart:
      enabled: true  # 开启热部署
      additional-paths: src/main/java # 重启目录
      exclude: WEB-INF/**
  
  # 已有的配置保持不变...

3、如果使用 IDEA,还需要进行以下设置:

a. 开启自动构建:

  • 打开 Settings (Ctrl + Alt + S)
  • Build, Execution, Deployment > Compiler
  • 勾选 "Build project automatically"

b. 开启运行时自动构建:

  • 按下 Ctrl + Shift + Alt + / (Windows/Linux) 或 Command + Shift + A (Mac)
  • 选择 "Registry..."

  • 找到并勾选 "compiler.automake.allow.when.app.running"

4、在 IDEA 的运行配置中添加:

  • 打开运行配置(Run/Debug Configurations)

  • 在 Spring Boot 配置中的 "On Update action" 和 "On frame deactivation" 选择 "Update classes and resources"

5、配置完成后:

  • 重新构建项目

1、点击 IDEA 右侧的 Maven 工具窗口

2、点击工具栏中的"刷新"按钮(Reload All Maven Projects)

3、然后点击"清理并重新构建"按钮(Clean and Package)

  • 重启 Spring Boot 应用

6、现在当你修改后端代码时,应用会自动重新加载,不需要手动重启服务器。注意:

  • 修改 Java 文件会触发重启
  • 修改配置文件会触发重启
  • 修改静态资源不会触发重启,但会立即生效

因篇幅问题不能全部显示,请点此查看更多更全内容