点击上方“后端技术精选”,选择“置顶公众号”
技术文章第一时间送达!
平日里开发项目中,修改了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:
1dependency
2    groupIdorg.springframework.boot/groupId
3    artifactIdspring-boot-devtools/artifactId
4    optionaltrue/optional
5/dependency
devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),因为其采用的虚拟机机制,该项重启是很快的。
在Eclipse中生效还需要修改
spring-boot-maven-plugin插件:
 1build
 2    plugins
 3        plugin
 4            groupIdorg.springframework.boot/groupId
 5            artifactIdspring-boot-maven-plugin/artifactId
 6            configuration
 7                forktrue/fork
 8            /configuration
 9        /plugin
10    /plugins
11/build
并且开启Build Automatically:
测试热部署
在入口类中添加一个方法,用于热部署测试:
 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
END
Java面试题专栏
 
欢迎长按下图关注公众号后端技术精选
 
原文始发于微信公众号(后端技术精选):
 本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!
        本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!
     
                        
                        