SpringBoot框架实现热部署配置方法

SpringBoot项目实现热部署方法

最近在小编在开发中用到了热部署,在这里做如下记录:

一、什么是热部署?
所谓热部署,就是在应用正在运行的时候升级软件,却不需要重新启动应用。

二、什么是SpringBoot热部署?
通俗的说,就是当你在修改SpringBoot项目中的静态资源的时候,不需要每一次都要去重启项目才能达到预期效果。

三、怎么使用SpringBoot热部署?
1.pom文件中导入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>
        <!-- servlet依赖. -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <!--SpringBoot热部署配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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.在Settings…中设置自动编译
首先ctrl+alt+s打开设置,在Settings…中搜索Compliler,勾选Build project automatically,如下图所示:
在这里插入图片描述
4.快捷键Ctrl + Shift + Alt + /,出现如下图所示界面,点击Registry…,如下图:在这里插入图片描述
点击进入后,勾选compiler.automake.allow.when.app.running后Close即可
在这里插入图片描述
以上步骤,就完成了SpringBoot项目的热部署功能!
5.测试

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 "HelloWorld!";
    }
}

启动项目,通过浏览器输入地址:http://127.0.0.1:8080/hello/index

在这里插入图片描述
修改文件

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";
    }
}

测试热部署是否成功,结果如下:

在这里插入图片描述
一次次的重启项目,实在受不了,有了SpringBoot热部署后大大提高了开发效率,因为频繁的重启项目,势必会浪费很多时间。故写下这篇博客,希望可以帮助到大家,谢谢支持!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Monster_起飞

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

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

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

打赏作者

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

抵扣说明:

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

余额充值