在IDEA上对SpringBoot项目配置Devtools实现热部署


spring为开发者提供了一个名为spring-boot-devtools的模块来使Spring Boot应用支持热部署,提高开发者的开发效率,无需手动重启Spring Boot应用。


devtools实现原理:

  • 采用双类加载机制
    - base ClassLoader : 加载第三方jar包(很少修改)
    - restart ClassLoader :加载自己创建的类文件
    应用重启时只需要重新new一个restart ClassLoader即可,加载jar包的base ClassLoader无需加载,所以devtools重启速度很快,提升效率

1.在pom文件中添加devtools依赖


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

2.idea自动编译设置

  • File -> Settings -> Build,Execution,Deplyment -> Compiler,选中打勾 “Build project automatically”
  • Shift+Ctrl+Alt+/ => 选择Registry => 找到“compiler.automake.allow.when.app.running”并选中

tips : 我设置自动编译后,devtools在我修改完文件后反应有点迟缓,经常要在修改过后等个几秒钟才开始自动编译,暂时没找到具体原因,有知道的小伙伴可以在评论区告诉我一下,如果你在设置完后也有这种情况,可以进行手动编译,修改完内容后按 Ctrl+Shift+F9编译文件

3.修改启动器配置

  • Edit Configurations中设置 Running Application Update Policies 热更新策略