SpringBoot热部署详解

博客主页:不会压弯的小飞侠
✨欢迎关注:点赞收藏⭐留言✒
✨系列专栏:SpringBoot专栏(每日更新)
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
欢迎大佬指正,一起 学习!一起加油!
SpringBoot热部署详解_第1张图片


目录

  • 前言
  • 为什么要使用热部署
  • 手动启动热部署
    • 导入坐标 - 启动开发者工具
    • 修改数据
    • build project
    • 测试
  • 自动启动热部署
  • 热部署范围配置
  • 禁用热部署
    • 方式一
    • 方式二


前言

热部署,就是不需要停掉服务,可以线上改,改完立马生效。


为什么要使用热部署

因为不启用热部署时每次更改java数据都要重启服务器影响开发效率。

关于热部署:

  • 重启(Restart)∶自定义开发代码,包含类、页面、配置文件等,加载位置restart类加载器
  • 重载(ReLoad) : jar包,加载位置base类加载器

手动启动热部署

导入坐标 - 启动开发者工具

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

修改数据

方便测试是否启用了热部署:
详细代码:点击直接查看

 @GetMapping("{id}")
    public R getById(@PathVariable Integer id){
        System.out.println("host deploy...");
        System.out.println("host deploy...");
        System.out.println("host deploy...");
        return new R(true, bookService.getById(id));
    }

build project

也可以使用快捷键:ctrl + shift9
SpringBoot热部署详解_第2张图片

测试

SpringBoot热部署详解_第3张图片

自动启动热部署

自动启动热部署步骤:

  1. file
  2. setting
  3. 搜索Compiler,在右侧勾上 “ Build project automaticallySpringBoot热部署详解_第4张图片
  4. 快键键Ctrl+atl+shift+/
  5. 点击Registry SpringBoot热部署详解_第5张图片
  6. 勾选第一行这个如下图 SpringBoot热部署详解_第6张图片

热部署范围配置

如果想要某些文件或者文件夹不参与热部署的配置需要在application.xml中配置以下信息:

# 设置不参与热部署的文件或文件夹
devtools:
  restart:
    exclude: static/**,public/**,config/application.yml

禁用热部署

方式一

在application.yml中配置:

# 设置不参与热部署的文件或文件夹
devtools:
  restart:
    exclude: static/**,public/**,config/application.yml
    enabled: false

这种形式关闭热部署,优先级别太低,可能关闭之后,别人又从别的配置文件或者其他地方给打开了(在优先级别高的地方),从而导致热部署在此启动.

方式二

在优先级别高的地方禁用热部署。

  • 属性加载优先顺序:由低到高
    • 1 Default properties (specified by setting springApplication.setDefaultproperties )
    • 2 GPropertySsource annotations on your @Cconfiguration classes. Please note that such property sources are not added to theEnvironment until the application context is being refreshed.This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.
    • 3 Config data (such as application.properties files)
    • 4 A RandomValuePropertySource that has properties only in random.* .
    • 5 OS environment variables.
    • 6 Java System properties ( system.getProperties() ).
    • 7 JNDl attributes from java:comp/env .
    • 8 ServletContext init parameters.
    • 9 Servletconfig init parameters.
    • 10 Properties from spRING_APpLICATION_soN (inline JSON embedded in an environment variable or system property).
    • 11 Command line arguments.
    • 12 properties attribute on your tests.Available on gSpringRootTest and the test annotations for testing a particular slice ofyour application.
    • 13 @TestPropertySource annotations on your tests.
    • 14 Devtools global settings properties in the SHN’E .config/spring-boot directory when devtools ,s.ati

application.yml配置文件在优先级为3的地方,可以在优先级为6的地方禁用热部署功能:

package com.jkj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootHotDeployApplication {
    public static void main(String[] args) {
        System.setProperty("spring.devtools.restart.enabled","false");
        SpringApplication.run(SpringbootHotDeployApplication.class);
    }

}

SpringBoot热部署详解_第7张图片

你可能感兴趣的:(SpringBoot,spring,boot,java,spring)