18、最佳实践-dev-tools

dev-tools

热加载自动重启、实时刷新页面。

通过热加载的方式实现应用的自动重启以及和浏览器配合使用,实现热更新后页面的自动刷新,提升开发效率。

Spring Boot includes an additional set of tools that can make the application development experience a little more pleasant. The spring-boot-devtools module can be included in any project to provide additional development-time features.—— link

Applications that use spring-boot-devtools automatically restart whenever files on the classpath change. This can be a useful feature when working in an IDE, as it gives a very fast feedback loop for code changes. By default, any entry on the classpath that points to a directory is monitored for changes. Note that certain resources, such as static assets and view templates, do not need to restart the application.—— link

Triggering a restart
As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. The way in which you cause the classpath to be updated depends on the IDE that you are using:
In Eclipse, saving a modified file causes the classpath to be updated and triggers a restart.
In IntelliJ IDEA, building the project (Build -> Build Project)(shortcut: Ctrl+F9) has the same effect.

一,自动重启以及与实现原理

  1. 添加依赖:
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

在IDEA中,项目或者页面修改以后:Ctrl+F9。

  1. devtools 实现应用的自动重启

devtools 可以对classpath 下的所有文件进行监控,当classpath下面的文件发现改变时,进行自动重启

  • idea触发自动重启的条件为:

  • Build -> Build Project

  • eclipse 触发自动重启的条件为:

  • 保存代码

其实都需要手动触发 Build 才可以,自动重启,原理在后面解释

静态文件修改不需要自动重启,可以通过如下参数设置,排斥自动加载

spring.devtools.restart.exclude=static/**,public/**
如果你使用的是 maven 或者gradle 自动重启, 需要将参数 forking 设置为enabled, devTools依赖于应用程序上下文的shutdown钩子来在重启期间关闭它。如果禁用了shutdown钩子,它就不能正常工作 (SpringApplication.setRegisterShutdownHook(false))
3. dev-tools 热加载实现原理:

Restart 技术(自动重启)采用了两个类加载器实现:

  1. base 类加载器

对于有的class是不会被改变的,比如第三方依赖jar,这部分class被加载到一个 base 类加载器中

  1. restart类加载器

自己日常开发的class文件则被加载到另外一个 restart类加载器中。

重启的时候,直接销毁掉restart类加载器,重新创建一个新的restart类加载器,从而实现快速重启的功能,因为 base 类加载器中的类没有发生改变,加载一次就好了。

如果您发现重新启动对您的应用程序来说不够快,或者遇到类加载问题,您可以考虑ZeroTurnaround 公司的重加载技术 JRebel。这些方法是在加载类时重写它们,使它们更易于重新加载

当你使用java -jar 形式启动springboot 项目时 ,dev-tools 将被自动禁用(dev-tools

根据使用的类加载器来判断是否启用,springboot以jar启动时使用的是自定义类加载器),

你也可以通过系统参数进行启用,这时dev-tools 将忽略你所使用的类加载器类型:

    -Dspring.devtools.restart.enabled=true   

dev-tools 适合在开发环境下使用,不建议在生产中启用。

二,页面自动刷新

页面自动刷新需要和浏览器插件配合使用。

比如chrome 浏览器需要安装 LiveReload

浏览器插件装好后,启动springboot 项目,修改完静态文件,然后触发重新编译,页面将自动刷新,触发重新编译,不同的编译器,不同的方式,和上面的自动重启是一样的。

我们以Idea为例, 引入了dev-tools 插件的 SpringBoot 项目启动时,会同时启动一个 liveReload server 服务(这个服务为后端服务与前端浏览器插件进行交互的 websocket 服务),可以通过以下日志验证。

修改文件后,重新编译项目,如下图

重新编译后,前端页面自动刷新,通过网络监控可以看到,这个是一个websocket协议通知触发的自动刷新


引用原文:https://zhuanlan.zhihu.com/p/374889935

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值