springboot热部署
1.spring-boot-devtools 实现热部署
spring-boot-devtools 最重要的功能就是热部署。它会监听 classpath 下的文件变动,并且会立即重启应用。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
optional=true 表示依赖不会传递,换句话说,其他依赖该项目的项目,如果想要使用 devtools,需要重新引入
如果,希望指定文件夹下的文件改变的时候,重新启动 Spring Boot,我们只要在 src/main/resources/application.properties 中配置信息。
spring.devtools.restart.additional-paths= # Additional paths to watch for changes.
自定义配置热部署
以下配置用于自定义配置热部署,可以不设置。
# 热部署开关,false即不启用热部署 spring.devtools.restart.enabled: true # 指定热部署的目录 #spring.devtools.restart.additional-paths: src/main/java # 指定目录不更新 spring.devtools.restart.exclude: test/**
如果是Intellij Idea,需要改以下两个地方:
1、勾上自动编译或者手动重新编译
File > Settings > Compiler-Build Project automatically
2、注册
ctrl + shift + alt + / > Registry > 勾选Compiler autoMake allow when app running
注意事项
1、生产环境devtools将被禁用,如java -jar方式或者自定义的类加载器等都会识别为生产环境。
2、打包应用默认不会包含devtools,除非你禁用SpringBoot Maven插件的 excludeDevtools
属性。
3、Thymeleaf无需配置 spring.thymeleaf.cache:false
,devtools默认会自动设置,参考完整属性。
https://github.com/spring-projects/spring-boot/blob/v1.5.7.RELEASE/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsPropertyDefaultsPostProcessor.java
4、devtools会在windows资源管理器占用java进程,在开发工具里面杀不掉,只能手动kill掉,不然重启会选成端口重复绑定报错。
2.Spring Loaded 实现热部署
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.6.RELEASE</version> </dependency> </dependencies> </plugin>
使用 mvn spring-boot:run 启动项目。
官方文档:https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构