SpringBoot热部署说明和设置步骤

一、什么是“热部署”

        看下图  :

               “热部署”,即在应用正在运行的时候升级软件(内容变动),不需要重新启动应用。 

二、SpringBoot 热部署 

       对于 Java 应用程序来说,热部署就是 “ 在运行时更新Java类文件 ”。    

        基于热部署概念,SpringBoot热部署就是指 “ 修改SpringBoot 中Java类文件/静态资源内容” 时,不需要每一次都重启项目才能达到我们需要的效果。看下图:

三、SpringBoot热部署方法

1. 在SpringBoot项目的 pom.xml 文件中导入spring-boot-devtools依赖。

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        
        <!-- 开启热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <!-- 设置为true 热部署才有效 -->
            <optional>true</optional>
            <scope>true</scope>
        </dependency>



    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.在项目的 application.properties 配置文件中设置启动热部署

#配置项目热部署
spring.devtools.restart.enabled=true

3. 点击 File --->> Settings    --->>  Build,Execution,Deployment --->> Compiler --->> 勾选 Build project automatically  ,如下图所示。

4.按下键盘 " Ctrl + Shift + Alt + /  " (必须都按下才会弹出弹窗),

    点击 Registry… ( 即:打开注册表)选项。

5.在 Registry 面板上 找到 并 勾选 "compiler.automake.allow.when.app.running "  后,点击Close关闭,如下图。

6.测试是否热部署成功

(1)未修改前内容

package com.example.springboot;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//测试 SpringBoot热部署

@RestController
@RequestMapping("/Hello")
public class HelloController{

    @RequestMapping("/index")
    public String index() {
        return "HelloWorld!"; //这是未进行热部署前的输出内容
    }
}

(2)修改输出的内容

package com.example.springboot;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController{

    @RequestMapping("/index")
    public String index() {

        //我们只是将 输出内容修改,测试热部署是否能帮我们自动更新内容
        return "Hello! Monster"; 

    }
}

7. 综上内容:SpringBoot热部署内容我们已经完成。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等日出看彩虹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值