2019独角兽企业重金招聘Python工程师标准>>>
SpringBoot入门——应用devtools进行热部署
1、devtools简介
devtools是boot的一个热部署工具,当我们修改了类文件、属性文件、页面、配置文件等时,会重新启动程序。
其原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方jar),另一个ClassLoader加载会更改的类,成为restart ClassLoader。
这样在有代码更改时候,原来的restart ClassLoader被丢弃,重新创建一个restart ClassLoader,由于需要加载的类比较少,所以实现了较快的重启时间(一般5秒内)。
2、使用方法
配置pom.xml添加依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
- maven中的optional=true表示依赖不会传递。即此处引用的devtools不会传递到依赖myboot项目的项目中。
- 仅仅加入devtools在我们的eclipse中还不起作用,这时候还需要对之前添加的spring-boot-maven-plugin做一些修改,如下:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
<configuration>
<!-- 如果没有该项配置,devtools不会起作用 -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
3、测试
为了方便测试,可以在SpringBoot项目中写个controller类
package com.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("/hello")
public String hello(){
return "hello world";
}
}
1、启动springboot
2、通过浏览器输入controller地址,获得返回的字符串
显示效果:
3、不关闭程序的情况下,修改返回的字符串值,然后刷新浏览器
@RequestMapping("/hello")
public String hello(){
return "hello friend";
}
显示效果:
在没有关闭程序的情况下发现字符串已经改变,测试成功