javascript
eclipse热部署_Spring Boot Devtools热部署
點擊上方“后端技術精選”,選擇“置頂公眾號”
技術文章第一時間送達!
作者:mrbird
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:
測試熱部署
在入口類中添加一個方法,用于熱部署測試:
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面試題專欄
【40期】說一下線程池內部工作原理【39期】Mybatis面試18問,你想知道的都在這里了!【38期】一份tcp、http面試指南,??键c都給你了【37期】請你詳細說說類加載流程,類加載機制及自定義類加載器【36期】說說 如何停止一個正在運行的線程?【35期】談談你對Java線程之間通信方式的理解【34期】談談為什么要拆分數據庫?有哪些方法?【33期】分別談談聯合索引生效和失效的條件【32期】你知道Redis的字符串是怎么實現的嗎?【31期】了解什么是 redis 的雪崩、穿透和擊穿?redis 崩潰之后會怎么樣?應對措施是什么歡迎長按下圖關注公眾號后端技術精選
總結
以上是生活随笔為你收集整理的eclipse热部署_Spring Boot Devtools热部署的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql8.0最低需要多少内存_MyS
- 下一篇: go 获取内核个数_图解Go运行时调度器