最近公司在開發項目時用到了熱部署,在這里做如下記錄:
一、什么是熱部署?
熱部署,就是在應用正在運行的時候升級軟件,卻不需要重新啟動應用。
二、什么是SpringBoot熱部署?
SpringBoot熱部署就是在項目正在運行的時候修改代碼, 卻不需要重新啟動項目。
有了SpringBoot熱部署后大大提高了開發效率,因為頻繁的重啟項目,勢必會浪費很多時間, 有了熱部署后,媽媽再也不用擔心我修改代碼重啟項目了~~~
三、SpringBoot熱部署的流程
1.pom文件中導入 spring-boot-devtools
依賴:
<!--SpringBoot熱部署配置 -->
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
2.繼續在pom.xml中添加插件:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <addResources>true</addResources> </configuration> </plugin> </plugins> </build>
3.設置application.properties
#配置項目熱部署 spring.devtools.restart.enabled=true
4.在idea中設置自動編譯:
首先ctrl+alt+s打開設置(Other Settings 的設置是對整個工作空間項目都啟作用,而Settings…的設置是對整個項目啟作用),搜索Compliler,勾選Build project automatically,如下圖所示:
5.按住ctrl + shift + alt + /,出現如下圖所示界面,點擊Registry...,如下圖:
點擊進入后,勾選compiler.automake.allow.when.app.running后關閉即可
通過以上步驟,就完成了SpringBoot項目的熱部署功能!!!
6.對熱部署測試是否成功:
package com.devtoolsDemo.devtoolsDemo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") public class HelloDemo { @RequestMapping("/index") public String index() { return "helloworld!"; } }
啟動項目,通過瀏覽器輸入地址:http://localhost:8080/hello/index
結果如下:
新加請求,在不重新啟動項目的情況下測試熱部署是否配置成功~~~
package com.devtoolsDemo.devtoolsDemo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") public class HelloDemo { @RequestMapping("/index") public String index() { return "helloworld!"; } @RequestMapping("/say") public String say(){ return "I love Java!"; } }
測試新加請求是否成功,瀏覽器輸入http://localhost:8080/hello/say后結果如下:
說明我們的熱部署配置生效啦~~~