在Spring Boot实现代码热部署是一件很简单的事情,代码的修改可以自动部署并重新热启动项目。

引用devtools依赖

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

这样,当修改一个java类时就会热更新。

自定义配置热部署

以下配置用于自定义配置热部署,可以不设置。

# 热部署开关,false即不启用热部署
spring.devtools.restart.enabled: true # 指定热部署的目录
#spring.devtools.restart.additional-paths: src/main/java # 指定目录不更新
spring.devtools.restart.exclude: test/**

Intellij Idea修改

如果是idea,需要改以下两个地方:

1、勾上自动编译或者手动重新编译

File > Settings > Compiler-Build Project automatically

2、注册

ctrl + shift + alt + / > Registry > 勾选Compiler autoMake allow when app running

注意事项

1、生产环境devtools将被禁用,如java -jar方式或者自定义的类加载器等都会识别为生产环境。

2、打包应用默认不会包含devtools,除非你禁用SpringBoot Maven插件的excludeDevtools属性。

3、Thymeleaf无需配置spring.thymeleaf.cache: false,devtools默认会自动设置,点击参考完整属性。

下面是devtools自动配置的部分源码:

@Order(Ordered.LOWEST_PRECEDENCE)
public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostProcessor { private static final Map<String, Object> PROPERTIES; static {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("spring.thymeleaf.cache", "false");
properties.put("spring.freemarker.cache", "false");
properties.put("spring.groovy.template.cache", "false");
properties.put("spring.mustache.cache", "false");
properties.put("server.session.persistent", "true");
properties.put("spring.h2.console.enabled", "true");
properties.put("spring.resources.cache-period", "0");
properties.put("spring.resources.chain.cache", "false");
properties.put("spring.template.provider.cache", "false");
properties.put("spring.mvc.log-resolved-exception", "true");
properties.put("server.jsp-servlet.init-parameters.development", "true");
PROPERTIES = Collections.unmodifiableMap(properties);
}

4、devtools会在windows资源管理器占用java进程,在开发工具里面杀不掉,只能手动kill掉,不然重启会选成端口重复绑定报错。

关于boot-devtools更多详细用法,点击参考官方文档。

推荐:Spring Boot & Cloud 最强技术教程

Spring Boot实现热部署的更多相关文章

  1. spring boot 调试 - 热部署

    maven gradle Maven: 命令行方式: mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport= ...

  2. spring boot 之热部署(三)

    热部署:当发现程序修改时自动启动应用程序. spring boot使用的是spring-boot-devtools是一个为开发者服务的一个模块.其原理用了classLoader 其中一个加载不变的类, ...

  3. 1. Spring boot 之热部署

    1. spring boot 热部署 1.1. springloaded springloaded可以实现修改类文件的热部署.下载地址:springloaded 安装单击Run Configurati ...

  4. spring boot 之热部署

    热部署:当发现程序修改时自动启动应用程序. spring boot使用的是spring-boot-devtools是一个为开发者服务的一个模块.其原理用了classLoader 其中一个加载不变的类, ...

  5. Spring boot的热部署

    当把配置文件,比如yml也打到jar包后,如何修改配置,而又不用重新发布呢? 在jar包同一目录下,放置Application.yml (注意,不管jar包内是否此文件名)修改配置文件后,重新启动ja ...

  6. IDEA下配置Spring Boot的热部署

    © 版权声明:本文为博主原创文章,转载请注明出处 devtools简介 spring-boot-devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),因为其采用的 ...

  7. Spring Boot (3) 热部署devtools

    热部署:当发现程序修改时自动启动应用程序. spring boot为开发者提供了一个名为spring-boot-devtools的模块来使sring boot应用支持热部署,提高开发者的开发效率,无需 ...

  8. Spring boot之热部署

    springboot热部署 1.springloaded(热部署) 2.devtools(热部署) 一.springloaded 问题的提出: 在编写代码的时候,你会发现我们只是简单把打印信息改变了, ...

  9. Idea+Spring boot 开启热部署

    热部署是spring boot的一大亮点功能,开发者不必因为改动一点代码就去频繁的关开服务. 1) 在pom文件中加载热部署依赖和插件 2)CTRL + SHIFT + A --> 查找make ...

随机推荐

  1. SAP 图形页面

    话不多说,先上炫图. *&---------------------------------------------------------------------* *& Repor ...

  2. vi怎么统计查找字符串的个数

    vi怎么统计查找字符串的个数 用vi打开一个比较大的文本,用vi查找指定字符串,现在怎么统计该字符串的个数呢?比如我查找ORA字符串,直接输入 /ORA的时候vi会高亮显示.现在怎么统计ORA的个数呢 ...

  3. NTFS(Windows)、ext4(RHEL6)和xfs(RHEL7)文件系统的误删除恢复和备份

    前言 对于误删除文件的设备,要马上停止任何写的操作,防止删除的文件被覆盖,导致数据丢失! 恢复NTFS文件系统下误删的文件 以Windows为例,市面上能恢复的工具不少,例如EasyRecovery. ...

  4. H3C网络设备配置SSH登录

    使用SSH+密码认证(基本SSH配置方法)注:在用户使用SSH登录交换机时,交换机对所要登录的用户使用密码对其进行身份验证生成RSA和DSA密钥对[H3C]public-key local creat ...

  5. HDU 5828 Rikka with Sequence(线段树区间加开根求和)

    Problem DescriptionAs we know, Rikka is poor at math. Yuta is worrying about this situation, so he g ...

  6. FortiGate安全策略说明

    1.安全策略原理 1)为了对数据流进行统一控制,方便用户配置和管理,FGT设备引入了安全策略的概念.通过配置安全策略,防火墙能够对经过设备的数据流进行有效的控制和管理. 2)当防火墙收到数据报文时,把 ...

  7. Springboot学习07-数据源Druid

    Springboot学习07-数据源Druid 关键字 Druid 前言 学习笔记 正文 1-Druid是什么 Druid是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池.插件框架和SQL解析器 ...

  8. 442. Find All Duplicates in an Array找出数组中所有重复了两次的元素

    [抄题]: Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and o ...

  9. [leetcode]200. Number of Islands岛屿个数

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  10. 探索未知种族之osg类生物--渲染遍历之GraphicsContext::runOperations

    osg::GraphicsContext::runOperations().我们先来看一下这个函数的执行过程. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...