spring为开发者提供了一个名为
spring-boot-devtools的模块来使Spring Boot应用支持热部署,提高开发者的开发效率,无需手动重启Spring Boot应用。
devtools实现原理:
- 采用双类加载机制
- base ClassLoader : 加载第三方jar包(很少修改)
- restart ClassLoader :加载自己创建的类文件
应用重启时只需要重新new一个restart ClassLoader即可,加载jar包的base ClassLoader无需加载,所以devtools重启速度很快,提升效率
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<!-- optional=true,依赖不会往下传递,
如果有项目依赖本项目,并且想要使用devtools,需要重新引入 -->
<optional>true</optional>
<scope>runtime</scope>
</dependency>
File->Settings->Build,Execution,Deplyment->Compiler,选中打勾 “Build project automatically”Shift+Ctrl+Alt+/=> 选择Registry=> 找到“compiler.automake.allow.when.app.running”并选中tips : 我设置自动编译后,devtools在我修改完文件后反应有点迟缓,经常要在修改过后等个几秒钟才开始自动编译,暂时没找到具体原因,有知道的小伙伴可以在评论区告诉我一下,如果你在设置完后也有这种情况,可以进行手动编译,修改完内容后按
Ctrl+Shift+F9编译文件
页面热部署
在application.properties文件中配置spring.thymeleaf.cache=false(页面修改后会立即生效)
不会重新启动的资源
/META-INF/maven, /META-INF/resources ,/resources ,/static ,/public 或者 /templates 不会触发重新启动, 但会触发实时重新加载。
关闭devtools
在Application中的
SpringApplication.run()方法之前添加System.setProperty("spring.devtools.restart.enabled", "false")
在IDEA上对SpringBoot项目配置Devtools实现热部署
原文:https://www.cnblogs.com/wuyiz/p/11718618.html