当前位置: 首页 > news >正文

SpringBoot依赖之Spring Boot DevTools热部署开发增效工具

摘要:Spring项目又大又重,依赖多,编译启动慢,怎么提高研发效率呢?方法之一==热部署==!

概念

Spring Boot DevTools
  • 依赖名称: Spring Boot DevTools
  • 功能描述: Provides fast application restarts, LiveReload, and configurations for enhanced development experience.
  • 中文释义:提供快速应用程序重启、LiveReload 和配置,以增强开发体验。

要在 IntelliJ IDEA 中创建一个 Spring Boot 项目,并集成 Spring Boot DevTools 实现热部署,您可以按照以下步骤操作。Spring Boot DevTools 可以在开发时自动重新加载应用程序,从而提高开发效率。

1. 创建 Spring Boot 项目

  1. 打开 IntelliJ IDEA

  2. 新建项目并添加添加 Spring Boot DevTools 依赖

  3. 打开 pom.xml 文件。

  4. <dependencies> 部分,添加以下依赖:

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    

    或者,如果您使用的是 Gradle,请在 build.gradle 文件中添加:

    dependencies {
        developmentOnly 'org.springframework.boot:spring-boot-devtools'
    }
    
  5. 刷新 Maven/Gradle 项目以确保依赖下载并生效。

2. 配置 IntelliJ IDEA 支持热部署

  1. 启用自动编译

    • 打开 File -> Settings -> Build, Execution, Deployment -> Compiler -> Build project automatically
    • 确保勾选 Build project automatically
      在这里插入图片描述
  2. 启用热部署

    • 在 Mac 上按下Command + Shift + A 或者按两下shift键,windows上 Ctrl + Shift + A,弹窗出来来后,搜索 Registry关键词。

在这里插入图片描述

  • 搜索并勾选启用 compiler.automake.allow.when.app.running

在这里插入图片描述

3. 编写简单的 Spring Boot 应用

文中涉及的学习代码地址

  1. 创建一个简单的 Controller 类来测试热部署:

    package com.dependencies.springbootdevtools;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author zhizhou   2024/8/29 12:40
     */
    @RestController
    public class HelloController {
        
        @GetMapping("/hello")
        public String hello() {
            return "Hello, I'm ok !";
        }
        
    }
    
  2. 创建主类 SpringBootDevtoolsApplication:

    package com.dependencies.springbootdevtools;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class SpringBootDevtoolsApplication {
        
        public static void main(String[] args) {
            SpringApplication.run(SpringBootDevtoolsApplication.class, args);
        }
        
    }
    
    

4. 运行和测试热部署

  1. 运行SpringBootDevtoolsApplication入口类
  2. 在浏览器中访问 http://localhost:8080/hello,您应该会看到 “Hello, I’m ok !”。
  3. 测试热部署:不需要停止运行的应用程序,直接在 HelloController 中修改返回的字符串为 “Hello, World!”。
  4. 保存文件,然后刷新浏览器,您应该会看到更新后的内容。

5. 总结

通过集成 Spring Boot DevTools,我们可以在开发时自动重新加载应用程序,而无需手动重启。这样可以极大地提高开发效率,尤其是在需要频繁修改代码并查看效果时。

文中涉及的学习代码地址

相关文章:

  • 【Spring Boot 3】【Web】解析获取HTTP请求参数
  • 2. 卷积神经网络(CNN):图像识别的核心技术
  • MySQL 数据库管理与操作指南
  • k8s集群本地搭建,使用gitlab、harbor、jenkens、cicd来实现devops自动化构建
  • c++中析构函数是否一定要为虚函数
  • 轴承知识大全,详细介绍(附3D图纸免费下载)
  • APP长文本内容编辑器功能实现方案
  • Linux 一个简单的中断信号实现
  • 【网络安全 | 渗透工具】Cencys+Shodan使用教程
  • 【C语言】---- 位操作处理
  • KCP实现原理探析
  • 【python计算机视觉编程——4.照相机模型与增强现实】
  • 【操作系统】线程同步之自旋锁
  • SQLite3 数据类型深入全面讲解
  • 猎板道出PCB免费打样真相:制造成本究竟给了谁?
  • AI技术颠覆游戏开发:谷歌DeepMind GameNGen实时生成《DOOM》探秘
  • Linux下安装MySQL8.0
  • 文本数据分析-(TF-IDF)(1)
  • MySQL——事务与存储过程(四)综合案例——存储过程应用
  • 【ESP8266】macos 下 sdk 环境搭建
  • 普雷沃斯特当选新一任天主教罗马教皇
  • 西南大学教授、重庆健美运动奠基人之一李启圣逝世
  • 比尔·盖茨:未来20年通过盖茨基金会捐出几乎全部财富,2045年底基金会停止运营
  • 家庭相册㉙在沪打拼25年,我理解了父母清晨去卖蜜饯的辛苦
  • 进化版大巴黎通杀英超,那个男人后悔了吗
  • 深圳下调公积金利率,209万纯公积金贷款总利息减少9.94万