Devtools热部署
实践
- 在pom.xml中配置spring-boot-devtools。注意: true务必写上。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
- Settings->Build,Execution,Deployment->Compiler->Build project automatically
- ctrl + shift + alt + /,选择Registry,勾选勾上 Compiler autoMake allow when app running
- 重启IDEA
原理
devtools会监听classpath下的文件变动,并会立即重启应用。
其中使用了两个ClassLoader,base ClassLoader用来加载那些不会变的类(如:第三方Jar包),restart ClassLoader加载更改的类,当有代码更改时,原来的restart ClassLoader被丢弃,重新创建一个restart ClassLoader。如此一来,由于需要加载的类比较少,所以实现了较快的重启。
LiveReload
LiveReload针对静态资源的自动更新。应用商店直接安装,使用时激活插件,页面和LiveReload间建立长连接,IDEA中修改前端内容,不用手动刷新,就会显示修改后的页面。
应用商店装不上的,可以百度.crx文件,改后缀名为.rar,解压为文件夹,在Chrome的扩展程序中加载已解压的拓展程序。