SpringBoot热部署加持

Stella981
• 阅读 496

SpringBoot热部署加持


概述

进行SpringBoot的Web开发过程中,我们很多时候经常需要重启Web服务器才能保证修改的 源代码文件、或者一些诸如xml的配置文件、以及一些静态文件生效,这样耗时又低效。所谓的热部署指的是我们不需要重启Web Server就可以保证各项文件修改的立即生效。下面便来介绍一下在IDEA中进行SpringBoot的Web开发时如何加入热部署的支持。

这里我们是使用 spring-boot-devtools 来帮助完成这个事情,其是一个为开发者服务的模块,可以实现SpringBoot热部署。其机理就是 spring-boot-devtools 会监听classpath下的文件变动,变动保存生效,其就会立即重启应用。

注: 本文首发于 My 公众号 CodeSheep ,可 长按扫描 下面的 小心心 来订阅 ↓ ↓ ↓

SpringBoot热部署加持



pom.xml中添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>


设置IDEA环境自动编译

首先在编译器选项中选中自动编译工程的选项,如下图所示,

SpringBoot热部署加持

接下来我们利用快捷键:Command + Shift + Alt + / 来调出Registry窗口,然后勾选其中的编译器自动编译选项,如下图所示:

SpringBoot热部署加持



启动项目并实验

上面几个步骤设置完成以后,我们去项目的根目录下执行命令来启动SpringBoot应用:

mvn clean spring-boot:run

此时的效果是只要我们修改了编辑区的源代码,保存之后,应用程序便会自动重启让修改生效。



监听文件夹的变化

上面我们是修改源代码时的自动热启动,除此之外,我们还可以设置 spring-boot-devtools 来监听某个文件夹的变化,比如我们想监听某个静态文件夹下文件的变化(如html文件),当其下面的文件改变的时候能够重新启动SpringBoot,那么我们只要在配置文件 application.properties 中添加如下配置即可:

spring.devtools.restart.additional-paths = src/main/resources/templates 

随后再次实验,取得的效果和之前一样,热启动便生效了



后记

作者更多的SpringBt实践文章在此:


如果有兴趣,也可以抽点时间看看作者一些关于容器化、微服务化方面的文章:



作者更多 务实、能看懂、可复现的 原创文章尽在公众号 CodeSheep,欢迎订阅 ⬇️⬇️⬇️

SpringBoot热部署加持


点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Stella981 Stella981
2年前
RPC框架实践之:Google gRPC
!MyDesktop(https://uploadimages.jianshu.io/upload_images/982424787bc1d746154471c.jpeg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)MyDesktop概述gRP
Easter79 Easter79
2年前
SpringBoot热部署加持
!Ultrafine5K(https://uploadimages.jianshu.io/upload_images/98242478bcc770c7a2f4a1f.png?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)概述进行SpringBoot的Web开发过程中
Stella981 Stella981
2年前
Docker容器跨主机通信之:直接路由方式
!Desktop(https://uploadimages.jianshu.io/upload_images/9824247a72fa1bbdcf60e4e.jpeg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)概述就目前Docker自身默认的网络来说
Stella981 Stella981
2年前
SpringBoot 中 @SpringBootApplication注解背后的三体结构探秘
!Profile(https://uploadimages.jianshu.io/upload_images/982424765c07db4e6eb5ceb.jpg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)概述SpringBoot约定大于配置的功
Stella981 Stella981
2年前
Spring Boot项目利用MyBatis Generator进行数据层代码自动生成
!Profile(https://uploadimages.jianshu.io/upload_images/9824247e1c1aaedc60d891f.jpg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)概述MyBatisGenerator(简称MB
Easter79 Easter79
2年前
SpringBoot应用部署于外置Tomcat容器
!Desktop(https://uploadimages.jianshu.io/upload_images/9824247cf33cdd281791244.jpg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)0x01.概述SpringB
Stella981 Stella981
2年前
Spring Boot Admin 2.0开箱体验
!Profile(https://uploadimages.jianshu.io/upload_images/9824247a282bed129783bfc.jpeg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)概述在我之前的《SpringBoot应用监控实战
Stella981 Stella981
2年前
Eureka Server 开启Spring Security Basic认证
!Desktop(https://uploadimages.jianshu.io/upload_images/98242475ce94f98ae00f42f.jpg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)文章共503字,阅读大约需要2分钟!概述
Stella981 Stella981
2年前
Spring Boot功能实战
!(http://uploadimages.jianshu.io/upload_images/5954965c7428bb7854e4e44.png?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)添加web功能启动器添加了SpringBoot基础依赖后,如要使用w