相关推荐recommended
idea设置自动编译spring boot代码,idea代码修改后无须重启服务立即生效
作者:mmseoamin日期:2024-04-29

一、背景

系统环境:windows 10

idea版本:IntelliJ IDEA 2022.2.3 (Ultimate Edition)

springBoot版本:2.2.6.RELEASE

在Spring Boot开发应用程序时,需经常要进行频繁的代码修改和调试。

为了更好的提高开发效率,希望能够自动编译和热更新应用程序,以便我们在修改代码后无需手动多次进行重新启动应用程序。

二、解决办法

1、在pom.xml文件中添加以下依赖项

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

spring-boot-devtools依赖项是Spring Boot的开发工具,它提供了自动编译和热更新的功能。

2、设置IDEA自动编译

打开IDEA,选择“File” -> “Settings” -> “Build, Execution, Deployment” -> “Compiler”。

在右侧一栏中,勾选“Build project automatically”选项。这样在保存文件时,IDEA将自动触发编译过程

idea设置自动编译spring boot代码,idea代码修改后无须重启服务立即生效,在这里插入图片描述,第1张

3、设置Spring Boot热更新

在IDEA中,选择“Run” -> “Edit Configurations”。

idea设置自动编译spring boot代码,idea代码修改后无须重启服务立即生效,在这里插入图片描述,第2张

在弹出的对话框中,选择您的Spring Boot应用程序配置,

点击右侧“Modify options”,找到“On frame deactivation”选项,并选择“Update classes and resources”。

idea设置自动编译spring boot代码,idea代码修改后无须重启服务立即生效,在这里插入图片描述,第3张

idea设置自动编译spring boot代码,idea代码修改后无须重启服务立即生效,在这里插入图片描述,第4张

这样,在IDEA失去焦点时,应用程序将自动更新类和资源。

4、运行Spring Boot应用程序

在IDEA中,打开“src/main/java”目录,并找到Spring Boot应用程序的入口类进行启动测试。