IDEA 2019配置SpringBoot实现热部署

后端学习456.7K4字数 1316阅读4分23秒阅读模式

Spring提供了一个名为 [code]spring-boot-devtools[/code] 的模块,使Spring Boot应用支持热部署,提高开发效率,无需手动重启Spring Boot应用。

 文章源自亦枫博客-https://yflad.cn/2290.html

一、配置pom.xml

1)加载maven依赖文章源自亦枫博客-https://yflad.cn/2290.html

<!--添加热部署-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
    <scope>true</scope>
</dependency>

optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入文章源自亦枫博客-https://yflad.cn/2290.html

或者将optional,scope两行换成<scope>runtime</scope>文章源自亦枫博客-https://yflad.cn/2290.html

2)开启热部署文章源自亦枫博客-https://yflad.cn/2290.html

<!--热部署配置-->
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <!--fork:如果没有该项配置,整个devtools不会起作用-->
        <fork>true</fork>
    </configuration>
</plugin>

 文章源自亦枫博客-https://yflad.cn/2290.html

二、配置IDEA

1)设置IDEA的自动编译:文章源自亦枫博客-https://yflad.cn/2290.html

“File”→“Settings([code]Ctrl+Alt+S[/code])”→“Build, Execution, Deplyment”→“Compiler”,选中“Build project automatically” 打勾文章源自亦枫博客-https://yflad.cn/2290.html

IDEA 2019配置SpringBoot实现热部署文章源自亦枫博客-https://yflad.cn/2290.html

2)组合键 [code]Shift+Ctrl+Alt+/[/code],选择 [code]Registry[/code] ,找到[code]compiler.automake.allow.when.app.running[/code],选中打勾。文章源自亦枫博客-https://yflad.cn/2290.html

(或者CTRL + SHIFT + A --> 查找Registry)文章源自亦枫博客-https://yflad.cn/2290.html

IDEA 2019配置SpringBoot实现热部署文章源自亦枫博客-https://yflad.cn/2290.html

 文章源自亦枫博客-https://yflad.cn/2290.html

三、游览器设置

设置游览器禁用缓存:F12(或Ctrl+Shift+J或Ctrl+Shift+I),打开开发者工具 → NetWork → 勾选[code]Disable Cache[/code]文章源自亦枫博客-https://yflad.cn/2290.html

IDEA 2019配置SpringBoot实现热部署文章源自亦枫博客-https://yflad.cn/2290.html

至此配置完成,修改文件后不用重启,刷新页面即可看到效果文章源自亦枫博客-https://yflad.cn/2290.html

 文章源自亦枫博客-https://yflad.cn/2290.html

四、原理

原理是在发现代码有更改之后,重新启动应用,但是速度比手动停止后再启动更快。深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为restart ClassLoader。这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,重新创建一个restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间。即devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机)文章源自亦枫博客-https://yflad.cn/2290.html

——摘抄文章源自亦枫博客-https://yflad.cn/2290.html

 文章源自亦枫博客-https://yflad.cn/2290.html

还有一篇相关文章写的也蛮好:点我到达

继续阅读
扫扫关注公众号
weinxin
我的微信
扫扫体验小程序
weinxin
我的公众号
亦枫
  • 本文由 发表于 2019年5月16日 00:14:10
评论  4  访客  3  作者  1
    • 手握日月摘星辰
      手握日月摘星辰 1

      按照上面的多做了但是没有一点用,很难受

      • 手握日月摘星辰
        手握日月摘星辰 1

        为什么我的热部署没作用 上面的多做到了还是没一点用

        • 楚狂人
          楚狂人 1

          好东西,可提高效率

        匿名

        发表评论

        匿名网友 填写信息

        :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

        确定

        取消