1. 引言
Spring Boot 让我们能够快速搭建和运行服务。为了进一步提升开发体验,Spring 在 Spring Boot 1.3 版本中发布了 spring-boot-devtools
工具。本文将介绍如何利用这个工具提升开发效率。
我们将涵盖以下内容:
- 默认属性配置
- 自动重启
- 实时重载
- 全局设置
- 远程应用支持
1.1. 在项目中添加 DevTools
引入 spring-boot-devtools
和添加其他 Spring Boot 模块一样简单。在现有项目中添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
执行项目构建后,DevTools 就集成完成了。最新版本可在 Maven 中央仓库 查找。
2. 默认属性配置
Spring Boot 会进行大量自动配置,包括默认开启缓存来提升性能。例如模板引擎(如 Thymeleaf)会缓存模板。但在开发阶段,我们更希望立即看到修改效果。
通常需要手动在 application.properties
中添加 spring.thymeleaf.cache=false
来禁用缓存。引入 DevTools 后,它会自动帮我们完成这项配置,无需手动干预。
3. 自动重启
传统开发流程中,修改代码后需要手动构建、部署/重启应用才能生效,或者依赖 JRebel 等工具。
使用 DevTools 后,这个过程完全自动化。当类路径下的文件发生变化时,应用会自动重启。这个特性显著缩短了验证修改所需的时间:
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. 实时重载
DevTools 模块内置了 LiveReload 服务器,当资源发生变化时,会触发浏览器刷新。
要使用此功能,需要在浏览器安装 LiveReload 插件。Chrome 用户可以安装 Remote Live Reload。
5. 全局设置
DevTools 支持配置不依赖特定应用的全局设置。配置文件名为 .spring-boot-devtools.properties
,位于 $HOME
目录下。
6. 远程应用支持
6.1. 通过 HTTP 远程调试(远程调试隧道)
DevTools 提供开箱即用的 HTTP 远程调试功能。要启用此功能,需确保 DevTools 被打包到应用中。在 Maven 插件中禁用 excludeDevtools
配置即可:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludeDevtools>false</excludeDevtools>
</configuration>
</plugin>
</plugins>
</build>
启用 HTTP 远程调试的步骤:
- 在服务器上启动应用时,需开启远程调试:
注意:这里未指定调试端口,Java 会随机选择端口-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n
- 在本地项目中,创建启动配置:
- 主类选择:
org.springframework.boot.devtools.RemoteSpringApplication
- 程序参数添加应用 URL,如
http://localhost:8080
- 主类选择:
- 默认调试端口为 8000,可通过以下配置覆盖:
spring.devtools.remote.debug.local-port=8010
- 创建远程调试配置,端口设置为
8010
(自定义)或8000
(默认)
启动后的日志示例:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ ___ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | | _ \___ _ __ ___| |_ ___ \ \ \ \
\\/ ___)| |_)| | | | | || (_| []::::::[] / -_) ' \/ _ \ _/ -_) ) ) ) )
' |____| .__|_| |_|_| |_\__, | |_|_\___|_|_|_\___/\__\___|/ / / /
=========|_|==============|___/===================================/_/_/_/
:: 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. 远程更新
远程客户端会监控应用类路径变化(类似远程重启功能)。类路径变更时,更新后的资源会被推送到远程应用并触发重启。
⚠️ 注意:只有远程客户端运行时才能监控文件变更并推送更新。
日志示例:
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
模块提升开发体验。它通过自动化大量开发任务,显著减少了开发时间,是 Spring Boot 开发者的必备工具。