使用方法
1.增加 DevTools 的依赖
maven
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies>
2.打开 Build project automatically 选项
使用 Ctrl
+Alt
+S
进入设置,然后找到 Compiler 选项,勾选 Build project automatically 选项
3.打开 IDEA 的 Registry 中的 compiler.automake.allow.when.app.running 选项
IntelliJ IDEA 新版本中 compiler.automake.allow.when.app.running 不存在的问题 - SpringCore - 博客园 (cnblogs.com)
很多文章介绍 IntelliJ IDEA 开启热部署功能都会写到在 IntelliJ IDEA 中的注册表中开启 compiler.automake.allow.when.app.running 选项,此选项在 IntelliJ IDEA 2021.2 之后的版本迁移到高级设置中。
先使用 Ctrl
+Alt
+Shit
+/
,在弹出的选项框中选择第一项,然后回车,然后勾选 compiler.automake.allow.when.app.running 选项
使用效果
当以上改动都生效后,在修改完某个类后,如果切出 IDEA 界面,比如切换到浏览器,IDEA 就会自动对比文件是否改动,然后重新编译部署,使用这种方法 base classloader 不会重新加载,因此比 stop 后重新 run 更快。