当前位置:主页 > 查看内容

spring-boot-devtools: 免费的热部署工具

发布时间:2021-08-16 00:00| 位朋友查看

简介:Jrebel很好用,但是要收费....... 热部署 监听到如果有Class文件改动了 就会创建一个新的ClaassLoader进行加载该文件 经过一系列的过程 最终将结果呈现在我们眼前 spring-boot-devtools 是一个为开发者服务的一个模块 其中最重要的功能就是自动将应用代码更改……

Jrebel很好用,但是要收费.......

热部署

监听到如果有Class文件改动了 就会创建一个新的ClaassLoader进行加载该文件 经过一系列的过程 最终将结果呈现在我们眼前

spring-boot-devtools

是一个为开发服务的一个模块 其中最重要的功能就是自动将应用代码更改到最新的App上面去 即在我们改变了一些代码或者配置文件的时候 应用可以自动重启 这在我们开发的时候 非常有用。

重新启动 vs 重新加载

Spring Boot提供的重启技术通过使用两个类加载器来工作。

不改变的类 例如来自第三方jar的类 被加载到base classloader中。

我们正在开发的类会加载到restart classloader中。当应用程序重新启动时 restart classloader将被丢弃并创建一个新类。这种方法意味着应用程序重启通常比 cold starts 快得多 因为基类加载器已经可用并且已经被填充。

spring-boot-devtools 使用maven依赖
 dependency 
 groupId org.springframework.boot /groupId 
 artifactId spring-boot-devtools /artifactId 
 scope runtime /scope 
 optional true /optional 
 /dependency 

image.gif

yml配置
# Spring
spring:
 devtools:
 restart:
 # 默认为true
 enabled: true

image.gif

?Idea配置代码自动编译

File -- Settings -- Compiler -- Build Project automatically

20210409163753521 (1).png

image.gif

运行期间自动编译

Mac使用快捷键shift option command / window上的快捷键是Shift Ctrl Alt / 打开Registry 勾选

compiler.automake.allow.when.app.running

20210409163807834.png

image.gif

通过以上的设置就可以在不重启服务的情况下加载html 但如果修改java文件 服务在几秒后会自动重启 如果不希望服务重启需要在application.properties或application.yml中添加spring.devtools.reatart.enable false

Thymeleaf模板引擎

如果使用Thymeleaf模板引擎 需要把模板默认缓存设置为false

#禁止thymeleaf缓存 建议 开发环境设置为false 生成环境设置为true 
# Spring
spring:
 thymeleaf:
 cache: false

image.gif

spring-boot-devtools 高级功能1. 排除资源

某些资源不一定需要在更改时触发重新启动。例如 可以就地编辑Thymeleaf模板。

默认情况下 在改变资源/META-INF/maven /META-INF/resources /resources /static /public 或 /templates不会触发重启但会触发 重新加载。可以使用spring.devtools.restart.exclude属性来自定义排除的资源。例如 要仅排除/static /public可以设置以下属性

spring.devtools.restart.exclude static/**,public/**

image.gif

2. 监控其他路径

如上所述 DevTools监控类路径资源的变动 但如果我们想更改不在类路径中的文件时重新启动或重新加载应用程序 该怎么办呢 这是可以使用spring.devtools.restart.additional-paths属性来配置其他路径以监视更改。

# Spring
spring:
 devtools:
 restart:
 # 默认为true
 enabled: true
 #排除那个目录的文件不需要restart
 additional-exclude: static/**,public/**
 #添加那个目录的文件需要restart
 additional-paths: src/main/java

image.gif

3. LiveReload

LiveReload在做前端开发的时候 经常会用到。

spring-boot-devtools模块包含嵌入式LiveReload服务器 可以在资源更改时用于触发浏览器刷新。 LiveReload浏览器扩展程序支持Chrome Firefox和Safari 你可以从livereload.com免费下载。

下面是Chrome的Remote Live Reload插件地址。安装即可拥有这个酷炫的功能。

https://chrome.google.com/webstore/detail/remotelivereload/jlppknnillhjgiengoigajegdpieppei?hl en-GB

image.gif

devtools也会在后台开启一个LiveReload Server 浏览器会与这个Server保持着一个长连接 当后端有前端资源变动的时候 将会通知浏览器进行刷新 实现热部署。

4.?远程调试

暂时没用到,后期补充


本文转自网络,原文链接:https://developer.aliyun.com/article/787054
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:MQTT X连接阿里云微服务消息队列MQTT 下一篇:没有了

推荐图文

  • 周排行
  • 月排行
  • 总排行

随机推荐