SpringBoot入门-开发环境热部署

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot入门-开发环境热部署相关的知识,希望对你有一定的参考价值。

参考技术A 我们在开发SpringBoot项目时,经常需要重启才能使新写的代码生效,特别是当项目变大时,一次重启就要一两分钟,相当耗时。有什么办法可以实现热部署呢?其实还是有的。

第一步,引入devtools依赖,需要注意的是这个依赖只能放在应用模块,不能放在父模块,否则会有问题

第二步,编译项目,每次编译项目,项目就会热部署,idea里面点击Build Project或者按Ctrl+F9都可以

第三步,测试,我把HelloController里面的输出从hello world改成hello world123,按ctrl+f9,看下控制台说明重启成功了

访问浏览器看到已经是修改后的返回值了

其实按照SpringBoot官方的说法这种方式不是严格意义的热部署,而是热重启,因为每次编译项目都会重启,那它和手动启动有啥区别呢?

热重启其实有两个ClassLoader,一个加载第三方库,一个加载工作区的类,热重启只会重新加载工作区的类,所以启动时间会大大缩短。看上图我们发现热重启启动只需1.15S,而冷启动需要3.5S

最后这个功能在idea有点bug就是有时会发现热部署后接口返回404,这时需要加一段配置

SpringBoot初始教程之热部署

SpringBoot初始教程之热部署(五)

1.介绍

SpringBoot提供了一个maven插件来支持热部署spring-boot-devtools,仅仅是在开发环境中使用,如果已经打包了就无法使用。


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

2.快速开始

完整pom


    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.1.RELEASE</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>

        <artifactId>springboot-5</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>1.4.1.RELEASE</version>
                    <configuration>
                        <fork>true</fork>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

基本配置就已经介绍了,使用上非常简便,修改完代码只需要重新编译即可。但是总的来说这种方式并不快,只是比重启快一些而已没有太大的速度提升
编译后会重新加载一遍,如果喜欢热部署推荐使用 JRebel这个有个人免费版本。

3.高级特性

如果你不想编译,可以指定监控目录一旦目录文件改变,就会触发重新加载


server:
  port: 8080
spring:
  devtools:
    restart:
      additional-paths:
        src/main/java/com/start

本文代码

https://git.oschina.net/wangkang_daydayup/SpringBoot-Learn/tree/master

springboot-5

以上是关于SpringBoot入门-开发环境热部署的主要内容,如果未能解决你的问题,请参考以下文章

SpringBootspringboot开发环境热部署

Spring Boot 开发环境热部署方案 !

springboot热部署——Java热部署与热加载原理

SpringBoot入门篇--热部署

SpringBoot实现热部署

SpringBoot2:开发实用篇(黑马程序员P67~P142)