spring boot 热部署_Spring Boot Devtools热部署

点击上方☝ SpringForAll社区  轻松关注! 及时获取有趣有料的技术文章

来源:

https://mrbird.cc/Spring-Boot-Devtools.html

平日里开发项目中,修改了Java代码或者配置文件的时候,必须手动重启项目才能生效。所谓的热部署就是在你修改了后端代码后不需要手动重启,工具会帮你快速的自动重启是修改生效。其深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为restart ClassLoader,这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,重新创建一个restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间。

本文将介绍如何通过使用Spring-Boot-devtools来实现Spring Boot项目的热部署。IDE使用的是Eclipse Oxygen,并且使用Maven构建。

引入Devtools

搭建一个简单的Spring Boot项目,然后引入Spring-Boot-devtools:

1<dependency>
2    <groupId>org.springframework.bootgroupId>
3    <artifactId>spring-boot-devtoolsartifactId>
4    <optional>trueoptional>
5dependency>

devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),因为其采用的虚拟机机制,该项重启是很快的。

在Eclipse中生效还需要修改spring-boot-maven-plugin插件:

 1<build>
2    <plugins>
3        <plugin>
4            <groupId>org.springframework.bootgroupId>
5            <artifactId>spring-boot-maven-pluginartifactId>
6            <configuration>
7                <fork>truefork>
8            configuration>
9        plugin>
10    plugins>
11build>

并且开启Build Automatically:

a5285d90e3ec8094c4f64d06d8b760d9.png
QQ截图20180523094924.png

测试热部署

在入口类中添加一个方法,用于热部署测试:

 1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3import org.springframework.web.bind.annotation.RequestMapping;
4import org.springframework.web.bind.annotation.RestController;
5
6@RestController
7@SpringBootApplication
8public class DemoApplication {
9    @RequestMapping("/")
10    String index() {
11        return "hello spring boot";
12    }
13    public static void main(String[] args) {
14        SpringApplication.run(DemoApplication.class, args);
15    }
16}

启动项目访问http://localhost:8080/,页面输出hello spring boot。

将方法的返回值修改为hello world并在保存的瞬间,应用便重启好了,刷新页面,内容也将得到更改。

所有配置

下面是所有Devtools在Spring Boot中的可选配置:

 1# Whether to enable a livereload.com-compatible server.
2spring.devtools.livereload.enabled=true 
3
4# Server port.
5spring.devtools.livereload.port=35729 
6
7# Additional patterns that should be excluded from triggering a full restart.
8spring.devtools.restart.additional-exclude= 
9
10# Additional paths to watch for changes.
11spring.devtools.restart.additional-paths= 
12
13# Whether to enable automatic restart.
14spring.devtools.restart.enabled=true
15
16# Patterns that should be excluded from triggering a full restart.
17spring.devtools.restart.exclude=META-INF/maven/**,META-INF/resources/**,resources/**,static/**,public/**,templates/**,**/*Test.class,**/*Tests.class,git.properties,META-INF/build-info.properties
18
19# Whether to log the condition evaluation delta upon restart.
20spring.devtools.restart.log-condition-evaluation-delta=true 
21
22# Amount of time to wait between polling for classpath changes.
23spring.devtools.restart.poll-interval=1s 
24
25# Amount of quiet time required without any classpath changes before a restart is triggered.
26spring.devtools.restart.quiet-period=400ms 
27
28# Name of a specific file that, when changed, triggers the restart check. If not specified, any classpath file change triggers the restart.
29spring.devtools.restart.trigger-file=

源码链接:https://github.com/wuyouzhuguli/Spring-Boot-Demos/tree/master/24.Spring-Boot-Devtools

参考自:http://412887952-qq-com.iteye.com/blog/2300313

278f855275f52267b04dc93fa7c1a673.png

● Spring Boot AOP记录用户操作日志

● Spring Boot整合Mongo DB

● 【图文讲解】你一定能看懂的HTTPS原理剖析!

●  基础面试,为什么面试官总喜欢问String?

●  Spring Boot Admin 2.2.0发布,支持最新Spring Boot/Cloud之外,新增中文展示!

●  你应该知道的 @ConfigurationProperties 注解的使用姿势,这一篇就够了

b4e94e41d98e90a92aa6d9f9c8184fca.png

2863e90829f8f7ed916bb185f6869c71.png

如有收获,请帮忙转发,您的鼓励是作者最大的动力,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值