初识spring boot热部署,就让我们一起来看看具体需要哪些基本的操作吧
- 首先,什么是热部署要搞明白,区别热部署和热加载的区别。 热加载,指得是在运行时实现对.class类的加载。 ***热部署***则实现的是对整个项目应用的加载,也就是加载了全部。
- 热部署更多的是在生产环境使用
热加载则更多的实在开发环境使用
热加载几乎没有安全性,直接修改Java虚拟机中的字节码文件,难以监控和控制
热加载有个通俗的名字就是开发者模式。 - 热部署在上线模式可以应用,而热加载则在生产模式下使用,上线则不可用。
- 【说完原理,来看操作吧】
- spring-boot-devtools 是一个为开发者服务的模块,项目需要在pom.xml 中加入这个依赖,它其中最重要的 功能是自动实现吧更新的应用代码改到最新的APP上。
工作原理是在发现代码有更改之后,自动重新启动应用,但速度比手动要快。
举个小例 子,实现简单的部署操作。
pom.xml
···
4.0.0
org.fkit
devtoolstest
0.0.1-SNAPSHOT
jar
<!-- 添加spring-boot-starter-web模块依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加spring-boot-starter-thymeleaf模块依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Spring Boot spring-boot-devtools 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 如果没有该项配置,devtools不会起作用,即应用不会restart -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
处理器类
package org.fkit.devtoolstest.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
System.out.println("测试spring-boot-devtools热部署");
return "hello,devtools.....!";
}
}
当你改动输出结果时,在浏览器端刷新地址,则会看到结果发生改变了,并不需要手动重启服务。