pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<!-- 使用 <optional>true</optional> 也可,防止依赖传递到其他模块 -->
<optional>true</optional>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork> <!-- 如果没有此配置,devtools 可能不生效 -->
</configuration>
</plugin>
</plugins>
</build>
开启自动编译
-
打开
File->Settings(Windows/Linux) 或IntelliJ IDEA->Preferences(Mac) -
导航到
Build, Execution, Deployment->Compiler -
勾选
Build project automatically
配置项目的运行选项
-
点击 IDEA 顶部菜单
Run->Edit Configurations...。 -
选择你的 Spring Boot 应用配置。
-
在右侧窗口中,找到 "On 'Update' action" 和 "On frame deactivation" 两个选项(如果看不到,请点击 "Modify options" 来展开)。
-
将它们的值都设置为
Update classes and resources-
On 'Update' action:当你手动触发更新(快捷键Ctrl + F10/Cmd + F10,选择 Update)时的行为。 -
On frame deactivation:当 IDEA 窗口失去焦点时(例如你切换到浏览器窗口)的行为。
-
设置为 "Update classes and resources" 意味着无论是手动触发还是切换窗口,IDEA 都会尽力去更新类和静态资源。