前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot Devtools热部署

Spring Boot Devtools热部署

作者头像
技术从心
发布2019-11-23 08:43:22
6110
发布2019-11-23 08:43:22
举报
文章被收录于专栏:技术从心

来源:https://mrbird.cc/Spring-Boot-Devtools.html

平日里开发项目中,修改了Java代码或者配置文件的时候,必须手动重启项目才能生效。所谓的热部署就是在你修改了后端代码后不需要手动重启,工具会帮你快速的自动重启是修改生效。

其深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为restart ClassLoader,这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,重新创建一个restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间。

本文将介绍如何通过使用Spring-Boot-devtools来实现Spring Boot项目的热部署。IDE使用的是Eclipse Oxygen,并且使用Maven构建。

引入Devtools

搭建一个简单的Spring Boot项目,然后引入Spring-Boot-devtools:

代码语言:javascript
复制
<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-devtools</artifactId>    <optional>true</optional></dependency>

devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),因为其采用的虚拟机机制,该项重启是很快的。

在Eclipse中生效还需要修改spring-boot-maven-plugin插件:

代码语言:javascript
复制
<build>    <plugins>        <plugin>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-maven-plugin</artifactId>            <configuration>                <fork>true</fork>            </configuration>        </plugin>    </plugins></build>

并且开启Build Automatically:

测试热部署

在入口类中添加一个方法,用于热部署测试:

代码语言:javascript
复制
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublic class DemoApplication {    @RequestMapping("/")    String index() {        return "hello spring boot";    }    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }}

启动项目访问http://localhost:8080/,页面输出hello spring boot。

将方法的返回值修改为hello world并在保存的瞬间,应用便重启好了,刷新页面,内容也将得到更改。

所有配置

下面是所有Devtools在Spring Boot中的可选配置:

代码语言:javascript
复制
# Whether to enable a livereload.com-compatible server.spring.devtools.livereload.enabled=true # Server port.spring.devtools.livereload.port=35729 # Additional patterns that should be excluded from triggering a full restart.spring.devtools.restart.additional-exclude= # Additional paths to watch for changes.spring.devtools.restart.additional-paths= # Whether to enable automatic restart.spring.devtools.restart.enabled=true# Patterns that should be excluded from triggering a full restart.spring.devtools.restart.exclude=META-INF/maven/**,META-INF/resources/**,resources/**,static/**,public/**,templates/**,**/*Test.class,**/*Tests.class,git.properties,META-INF/build-info.properties# Whether to log the condition evaluation delta upon restart.spring.devtools.restart.log-condition-evaluation-delta=true# Amount of time to wait between polling for classpath changes.spring.devtools.restart.poll-interval=1s# Amount of quiet time required without any classpath changes before a restart is triggered.spring.devtools.restart.quiet-period=400ms# Name of a specific file that, when changed, triggers the restart check. If not specified, any classpath file change triggers the restart.spring.devtools.restart.trigger-file=

源码链接:https://github.com/wuyouzhuguli/Spring-Boot-Demos/tree/master/24.Spring-Boot-Devtools

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-11-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 技术从心 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 来源:https://mrbird.cc/Spring-Boot-Devtools.html
    • 引入Devtools
      • 测试热部署
        • 所有配置
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档