Spring Boot Dev Tools 概述

更新于 2025-12-30

baeldung 2024-01-10

Spring Boot Dev Tools 概述

1. 简介

Spring Boot 使我们能够快速搭建并运行服务。

为了进一步提升开发体验,Spring 在 Spring Boot 1.3 版本中发布了 spring-boot-devtools 工具。本文将介绍使用这一新功能所带来的优势。

我们将涵盖以下主题:

  • 属性默认值(Property defaults)
  • 自动重启(Automatic Restart)
  • 实时重载(Live Reload)
  • 全局设置(Global settings)
  • 远程应用(Remote applications)

1.1 在项目中添加 Spring Boot DevTools

在项目中添加 spring-boot-devtools 与添加其他 Spring Boot 模块一样简单。在现有的 Spring Boot 项目中,只需添加如下依赖项:

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

执行一次干净的构建(clean build),即可完成与 spring-boot-devtools 的集成。最新版本可在此处查看:Maven Central


2. 属性默认值

Spring Boot 提供了大量自动配置功能,包括默认启用缓存以提升性能。例如,模板引擎(如 Thymeleaf)会默认缓存模板文件。但在开发阶段,我们更希望立即看到修改效果。

通常,我们可以通过在 application.properties 文件中设置 spring.thymeleaf.cache=false 来禁用 Thymeleaf 缓存。但引入 spring-boot-devtools 后,这一操作会自动完成,无需手动配置。


3. 自动重启

在典型的开发环境中,开发者通常需要修改代码、重新构建项目,然后重新部署或启动应用,才能看到变更生效;或者借助 JRebel 等工具来实现热更新。

而使用 spring-boot-devtools 后,这一流程被自动化了:只要 classpath 中的文件发生变化,应用就会自动重启。这大大缩短了验证代码变更所需的时间。

日志示例:

19:45:44.804 ... - Included patterns for restart : []
19:45:44.809 ... - Excluded patterns for restart : [/spring-boot-starter/target/classes/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot/target/classes/, /spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/]
19:45:44.810 ... - Matching URLs for reloading : [file:/.../target/test-classes/, file:/.../target/classes/]

 :: Spring Boot ::        (v1.5.2.RELEASE)

2017-03-12 19:45:45.174  ...: Starting Application on machine with PID 7724 (<some path>\target\classes started by user in <project name>)
2017-03-12 19:45:45.175  ...: No active profile set, falling back to default profiles: default
2017-03-12 19:45:45.510  ...: Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@385c3ca3: startup date [Sun Mar 12 19:45:45 IST 2017]; root of context hierarchy

从日志可见,启动应用的线程不再是 main,而是 restartedMain。任何对项目的修改(包括 Java 文件)都会触发自动重启:

2017-03-12 19:53:46.204  ...: Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@385c3ca3: startup date [Sun Mar 12 19:45:45 IST 2017]; root of context hierarchy
2017-03-12 19:53:46.208  ...: Unregistering JMX-exposed beans on shutdown


 :: Spring Boot ::        (v1.5.2.RELEASE)

2017-03-12 19:53:46.587  ...: Starting Application on machine with PID 7724 (<project path>\target\classes started by user in <project name>)
2017-03-12 19:53:46.588  ...: No active profile set, falling back to default profiles: default
2017-03-12 19:53:46.591  ...: Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@acaf4a1: startup date [Sun Mar 12 19:53:46 IST 2017]; root of context hierarchy

4. 实时重载(Live Reload)

spring-boot-devtools 模块内置了一个 LiveReload 服务器,当资源文件(如 HTML、CSS、JS)发生变更时,可自动触发浏览器刷新。

要启用此功能,需在浏览器中安装 LiveReload 插件,例如 Chrome 浏览器可使用 Remote Live Reload 插件。


5. 全局设置

spring-boot-devtools 支持配置全局设置,这些设置不绑定于任何特定应用。配置文件名为 .spring-boot-devtools.properties,位于用户主目录($HOME)下。


6. 远程应用

6.1 通过 HTTP 进行远程调试(Remote Debug Tunnel)

spring-boot-devtools 提供了开箱即用的基于 HTTP 的远程调试能力。要启用此功能,需确保 spring-boot-devtools 被打包进最终的应用程序中。在 Maven 项目中,可通过如下方式禁用默认的排除行为:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludeDevtools>false</excludeDevtools>
            </configuration>
        </plugin>
    </plugins>
</build>

启用远程调试需执行以下步骤:

  1. 在服务器上启动应用时启用远程调试

    -Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n
    

    注意:此处未指定端口,JVM 将自动选择一个随机端口。

  2. 在本地 IDE 中配置启动项

    • 主类(Main Class):org.springframework.boot.devtools.RemoteSpringApplication
    • 程序参数(Program arguments):填写远程应用的 URL,例如 http://localhost:8080
    • 默认调试端口为 8000,可通过以下属性覆盖:
      spring.devtools.remote.debug.local-port=8010
      
  3. 创建远程调试配置,监听上述端口(8000 或 8010)。

日志示例:

  .   ____          _                                              __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _          ___               _      \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` |        | _ \___ _ __  ___| |_ ___ \ \ \ \
 \\/  ___)| |_)| | | | | || (_| []::::::[]   / -_) '  \/ _ \  _/ -_) ) ) ) )
  '  |____| .__|_| |_|_| |_\__, |        |_|_\___|_|_|_\___/\__\___|/ / / /
 =========|_|==============|___/===================================/_/_/_/
 :: Spring Boot Remote ::  (v1.5.2.RELEASE)

2017-03-12 22:24:11.089  ...: Starting RemoteSpringApplication v1.5.2.RELEASE on machine with PID 10476 (..\org\springframework\boot\spring-boot-devtools\1.5.2.RELEASE\spring-boot-devtools-1.5.2.RELEASE.jar started by user in project)
2017-03-12 22:24:11.097  ...: No active profile set, falling back to default profiles: default
2017-03-12 22:24:11.357  ...: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@11e21d0e: startup date [Sun Mar 12 22:24:11 IST 2017]; root of context hierarchy
2017-03-12 22:24:11.869  ...: The connection to http://localhost:8080 is insecure. You should use a URL starting with 'https://'.
2017-03-12 22:24:11.949  ...: LiveReload server is running on port 35729
2017-03-12 22:24:11.983  ...: Started RemoteSpringApplication in 1.24 seconds (JVM running for 1.802)
2017-03-12 22:24:34.324  ...: Remote debug connection opened

6.2 远程更新

远程客户端会像本地开发一样监控 classpath 的变化。一旦检测到变更,就会将更新后的资源推送到远程应用,并触发重启。

注意:只有在远程客户端运行时,才会监控文件变化并推送更新。

日志示例:

2017-03-12 22:33:11.613  INFO 1484 ...: Remote debug connection opened
2017-03-12 22:33:21.869  INFO 1484 ...: Uploaded 1 class resource

7. 结论

通过本文的简要介绍,我们展示了如何利用 spring-boot-devtools 模块显著提升开发体验,并通过自动化大量重复性任务来缩短开发周期。