SpringBoot开发模式自动重启热部署spring-boot-devtools

简介: SpringBoot开发模式自动重启热部署spring-boot-devtools

有几篇不错的文章


https://springframework.guru/spring-boot-developer-tools/

https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.devtools

Triggering a restart

As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. The way in which you cause the classpath to be updated depends on the IDE that you are using:


  • In Eclipse, saving a modified file causes the classpath to be updated and triggers a restart.
  • In IntelliJ IDEA, building the project (Build +→+ Build Project) has the same effect.


简而言之就是,不同IDE触发重启方式不一样

  • Eclipse保存文件既可以触发重启
  • IntelliJ IDEA 需要重新构建项目

热部署

1、页面热部署

application.properties文件中配置

spring.thymeleaf.cache=false

2、类文件热部署

spring-boot-devtools

1、添加依赖

<dependency>

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

实际使用发现,不用配置application.yml也可以实现自动重启

2、修改配置application.yml

spring:
devtools:
restart:
enabled: true

3、将项目设置为自动加载

1、打开设置 勾选 Build project automatically

image.png

2、打开 Maintenance 选择 1.Register

快捷键 (一起按下四个按键)

windows: Ctrl + Shift +Alt + /

Mac : command + shift + option + /

6.png

3、勾选 compiler.automake.allow.when.app.running

image.png

IDEA过一会就会响应变化重启服务,如果想要快速触发,就重新编译


这里分享一份技巧:


保存文件的快捷键是 command + S


设置自动构建后需要等待一会才进行自动构建,这样明显不能等。

可以设置构建文件的快捷键:control + S,此时可以不设置自动构建,修改文件后手动进行构建。

Live Reload

Chrome插件 Live Reload 支持前端页面开发修改文件后自动刷新页面

注意:html、css、js文件同样需要build 到classpath路径下

总结:Spring Boot Live Reload

一、后端

使用依赖, 无需设置

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<!--当前这个项目被继承之后,这个不向下传递-->
<optional>true</optional>
</dependency>

java 代码的修改重启服务

二、前端

配合Chrome浏览器插件 Live Reload,无需设置

前端代码html、js、css文件的修改浏览器自动刷新

参考
SpringBoot热部署自动重启 spring-boot-devtools
            </div>
目录
相关文章
|
18天前
|
缓存 前端开发 Java
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)
Soring Boot的起步依赖、启动流程、自动装配、常用的注解、Spring MVC的执行流程、对MVC的理解、RestFull风格、为什么service层要写接口、MyBatis的缓存机制、$和#有什么区别、resultType和resultMap区别、cookie和session的区别是什么?session的工作原理
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)
|
2月前
|
缓存 NoSQL Java
SpringBoot的三种缓存技术(Spring Cache、Layering Cache 框架、Alibaba JetCache 框架)
Spring Cache 是 Spring 提供的简易缓存方案,支持本地与 Redis 缓存。通过添加 `spring-boot-starter-data-redis` 和 `spring-boot-starter-cache` 依赖,并使用 `@EnableCaching` 开启缓存功能。JetCache 由阿里开源,功能更丰富,支持多级缓存和异步 API,通过引入 `jetcache-starter-redis` 依赖并配置 YAML 文件启用。Layering Cache 则提供分层缓存机制,需引入 `layering-cache-starter` 依赖并使用特定注解实现缓存逻辑。
346 1
SpringBoot的三种缓存技术(Spring Cache、Layering Cache 框架、Alibaba JetCache 框架)
|
2月前
|
Java 微服务 Spring
SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
文章介绍了如何利用Spring Cloud Alibaba快速构建大型电商系统的分布式微服务,包括服务限流降级等主要功能的实现,并通过注解和配置简化了Spring Cloud应用的接入和搭建过程。
SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
|
3月前
|
监控 Java 应用服务中间件
什么是 Spring Boot,及为什么要用 Spring Boot
**Spring Boot**: 2013年起研,简化Spring笨重配置,集成常用库,开箱即用,少代码配置,专注业务。 **为何选Spring Boot?** 出色基因,快速搭建;单一依赖替多;Java Config简化配置;内嵌Tomcat,简化部署;监控REST化;微服务友好,趋势之选。
108 27
|
2月前
|
安全 Java 数据安全/隐私保护
基于SpringBoot+Spring Security+Jpa的校园图书管理系统
本文介绍了一个基于SpringBoot、Spring Security和JPA开发的校园图书管理系统,包括系统的核心控制器`LoginController`的代码实现,该控制器处理用户登录、注销、密码更新、角色管理等功能,并提供了系统初始化测试数据的方法。
36 0
基于SpringBoot+Spring Security+Jpa的校园图书管理系统
|
2月前
|
安全 Java 数据库
|
2月前
|
JSON 安全 Java
|
2月前
|
前端开发 Java 开发者
Spring Boot DevTools 热部署神器,助你在开发浪潮中乘风破浪,一骑绝尘!
【8月更文挑战第29天】在快速迭代的软件开发领域,高效开发至关重要。Spring Boot DevTools 作为一款优秀的热部署工具,可自动重新加载代码修改,无需手动重启应用,大幅节省时间,即时预览修改效果,简化开发流程。通过简单示例,展示了其自动刷新静态资源和模板文件的功能,有效提升了开发效率,使开发者更专注于功能实现。它就像是开发者的得力助手,显著减少等待时间,带来更高效、流畅的开发体验。
31 0
|
3月前
|
Java Spring 容器
Spring Boot 启动源码解析结合Spring Bean生命周期分析
Spring Boot 启动源码解析结合Spring Bean生命周期分析
85 11
|
2月前
|
Java Spring
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息