平时使用SpringBoot开发项目的时候,如果要部署到服务器上,修改代码后需要上传jar包才能实现,这种方式比较麻烦!那么有没有什么办法能自动部署更新后的项目呢?今天给大家分享一款SpringBoot官方的热部署工具
spring-boot-devtools
,修改完代码后可自动完成热部署,非常方便!
简介
spring-boot-devtools
是SpringBoot官方提供的开发工具,如果你的应用集成了它,即可实现热部署和远程调试。使用该工具应用为什么启动更快了?主要是因为它使用了两种不同的类加载器。基础类加载器用于加载不会改变的类(比如第三方库中的类),重启类加载器用于加载你应用程序中的类。当应用程序启动时,重启类加载器中的类将会被替换掉,这就意味着重启将比冷启动更快!
热部署
接下来我们将在SpringBoot项目中集成devtools,来演示下热部署功能。
- 首先需要在项目的
pom.xml
中添加devtools的相关依赖;
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
- 为了方便测试,我们在项目中添加了一个测试接口,用于返回测试消息;
java
/**
* @auther macrozheng
* @description SpringBoot Dev Tools测试
* @date 2025/7/22
* @github https://github.com/macrozheng
*/
@Tag(name = "TestController", description = "SpringBoot Dev Tools测试")
@Controller
@RequestMapping("/test")
public class TestController {
@Operation(summary = "测试修改")
@RequestMapping(value = "/first", method = RequestMethod.GET)
@ResponseBody
public CommonResult first() {
String message = "返回消息";
return CommonResult.success(null,message);
}
}
- 然后启动项目,由于项目中集成了Swagger,可以通过Swagger生成的API文档页面访问接口,返回结果如下,访问地址:http://localhost:8088/swagger-ui.html
json
{
"code": 200,
"message": "返回消息",
"data": null
}
- 由于在项目构建时,devtools才会自动重启项目,而IDEA默认并没有开启自动构建,此时我们可以修改应用启动策略,设置当IDEA失去焦点时自动构建项目;

- 修改Controller中的代码,只要修改下
message
变量即可;
java
/**
* @auther macrozheng
* @description SpringBoot Dev Tools测试
* @date 2025/7/22
* @github https://github.com/macrozheng
*/
@Tag(name = "TestController", description = "SpringBoot Dev Tools测试")
@Controller
@RequestMapping("/test")
public class TestController {
@Operation(summary = "测试修改")
@RequestMapping(value = "/first", method = RequestMethod.GET)
@ResponseBody
public CommonResult first() {
String message = "返回消息(已修改)";
return CommonResult.success(null,message);
}
}
- 失去焦点后,再次访问测试接口,返回结果如下,证明修改后的代码已经被自动应用了。
json
{
"code": 200,
"message": "返回消息(已修改)",
"data": null
}
这或许是一个对你有用的开源项目,mall项目是一套基于
SpringBoot3
+ Vue 的电商系统(Github标星60K),后端支持多模块和最新微服务架构
,采用Docker和K8S部署。包括前台商城项目和后台管理系统,能支持完整的订单流程!涵盖商品、订单、购物车、权限、优惠券、会员、支付等功能!
- Boot项目:github.com/macrozheng/...
- Cloud项目:github.com/macrozheng/...
- 教程网站:www.macrozheng.com
项目演示:
远程调试
devtools除了支持热部署之外,还支持远程调试,接下来我们将把应用部署到Docker容器中,然后试试远程调试!
- 由于SpringBoot默认打包不会包含devtools,所以我们需要在
pom.xml
的SpringBoot maven插件配置中添加如下配置;
xml
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--打包时不排除Devtools-->
<excludeDevtools>false</excludeDevtools>
</configuration>
</plugin>
- 接下来修改
application.yml
,添加devtools的远程访问密码;
yaml
spring:
devtools:
remote:
secret: macro666
- 然后把项目打包成Docker镜像,使用如下命令运行起来;
bash
docker run -p 8088:8088 --name spring-devtools -d spring-examples/spring-devtools:1.0-SNAPSHOT
- 之后添加一个SpringBoot应用的运行配置,修改启动类为
org.springframework.boot.devtools.RemoteSpringApplication
,程序参数为http://192.168.3.101:8088
,配置信息具体如下;

- 启动该配置,控制台输出如下结果表示远程连接成功;
vbnet
2025-07-22T10:22:13.452+08:00 INFO 34364 --- [spring-devtools] [ main] o.s.b.devtools.RemoteSpringApplication : Starting RemoteSpringApplication v3.4.2 using Java 17.0.9 with PID 34364
2025-07-22T10:22:13.454+08:00 INFO 34364 --- [spring-devtools] [ main] o.s.b.devtools.RemoteSpringApplication : No active profile set, falling back to 1 default profile: "default"
2025-07-22T10:22:13.538+08:00 WARN 34364 --- [spring-devtools] [ main] o.s.b.d.r.c.RemoteClientConfiguration : The connection to http://192.168.3.101:8088 is insecure. You should use a URL starting with 'https://'.
2025-07-22T10:22:13.573+08:00 INFO 34364 --- [spring-devtools] [ main] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2025-07-22T10:22:13.584+08:00 INFO 34364 --- [spring-devtools] [ main] o.s.b.devtools.RemoteSpringApplication : Started RemoteSpringApplication in 0.352 seconds (process running for 0.739)
- 接下来我们再次修改下Controller中的测试代码,只要修改下
message
变量即可;
java
/**
* @auther macrozheng
* @description SpringBoot Dev Tools测试
* @date 2025/7/22
* @github https://github.com/macrozheng
*/
@Tag(name = "TestController", description = "SpringBoot Dev Tools测试")
@Controller
@RequestMapping("/test")
public class TestController {
@Operation(summary = "测试修改")
@RequestMapping(value = "/first", method = RequestMethod.GET)
@ResponseBody
public CommonResult first() {
String message = "返回消息(远程调试)";
return CommonResult.success(null,message);
}
}
- 远程调试如果自动构建的话会导致远程服务频繁重启,此时我们可以使用IDEA手动构建,在项目的右键菜单中可以找到构建按钮;

- 构建成功后可以发现远程服务会自动重启,并应用修改后的代码,访问测试接口返回如下信息,接口地址:http://192.168.3.101:8088/swagger-ui.html
json
{
"code": 200,
"message": "返回消息(远程调试)",
"data": null
}
总结
使用SpringBoot官方的devtools既可以实现热部署,也可以实现远程调试,反应速度还是非常快的,感兴趣的小伙伴可以尝试下它!