springboot开启热部署

在Spring Boot项目进行热部署测试之前,需要先在项目的pom.xml文件中添加spring-boot-devtools热
部署依赖启动器:
<!-- 引入热部署依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
由于使用的是IDEA开发工具,添加热部署依赖后可能没有任何效果,接下来还需要针对IDEA开发
工具进行热部署相关的功能设置
 
选择IDEA工具界面的【File】->【Settings】选项,打开Compiler面板设置页面
 

 

选择Build下的Compiler选项,在右侧勾选“Build project automatically”选项将项目设置为自动编
译,单击【Apply】→【OK】按钮保存设置
 
在项目任意页面中使用组合快捷键“Ctrl+Shift+Alt+/”打开Maintenance选项框,选中并打开
Registry页面,具体如图1-17所示
 

 

 

列表中找到“compiler.automake.allow.when.app.running”,将该选项后的Value值勾选,用于指
定IDEA工具在程序运行过程中自动编译,最后单击【Close】按钮完成设置
至此完成
 
实现热部署原理
这里对类加载采用了两种类加载器,对于第三方jar包采用base
classloader来加载,对于开发人员自己开发的代码则使用restartClassLoader来进行加载,这使得比停
掉服务重启要快的多,因为使用插件只是重启开发人员编写的代码部分。
@Component
public class Devtools implements InitializingBean {

@Override public void afterPropertiesSet() throws Exception {
System.out.println("guava-jar classLoader: " + DispatcherServlet.class.getClassLoader().toString());
System.out.println("devtoolds classLoader: " + Devtools.class.getClassLoader().toString());
}
}

 

 

 

某些资源在更改后不一定需要触发重新启动。例如,Thymeleaf模板可以就地编辑。默认情况下,改变
资源 /META-INF/maven , /META-INF/resources , /resources , /static , /public ,
或 /templates 不触发重新启动,但确会触发现场重装。如果要自定义这些排除项,则可以使用该
spring.devtools.restart.exclude 属性。例如,仅排除 /static , /public 您将设置以下属性:
 
spring.devtools.restart.exclude=static/**,public/**
posted @ 2021-05-11 11:11  奥里给  阅读(257)  评论(1编辑  收藏  举报