Spring Boot - devtools hot deployment

spring-boot-devtools is a set of development tools provided by Spring Boot, which is designed to improve the development experience. These tools include features such as automatic restart of applications, automatic refresh, and remote debugging. Here are the steps to integrate spring-boot-devtools into a Spring Boot application:

0. Enable the "Build project automatically" option

Insert image description here

1. Add necessary dependencies

 		<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. Add some DevTools configuration in application.yml

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

3. Let’s give a simple demonstration of 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. Modify the startup configuration: Check the box to perform updates when focus is lost.

Insert image description here

5. There will be a line like this in the startup log

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

Access interface:
Insert image description here
Modify it and visit again:
Insert image description here
If the log just cleared, you can see the first line of the console:

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

0 added 0 deleted 1 modified

Guess you like

Origin blog.csdn.net/qq_43116031/article/details/134471867