0.聲明
緣由:沒有學過或者沒有經歷SpringBoot的Application運行機制的話,一定會好奇,博主為啥會寫一篇關閉開啟的博文,是不是好幼稚?(/o(╥﹏╥)o),待我娓娓道來......為什么需要關閉Application?因為它每運行一次就占用一個端口啊!!!!(殊不知,在關閉不掉這端口的時候是多痛苦) 待下一次應用再次調試啟動時,就端口沖突了....而Application開啟后又極其難關閉,用任務管理器是殺不死這個Web Application小強的。在此之前,只能通過關閉IDE的方式關閉,而查詢網友文獻,均沒有具體說清楚具體如何操作,都是把文章抄過來抄過去,連一個字都沒有變,這怕也是國內知識(保護意識不強,版權意識不強所導致的)抄襲成風的劣根性吧!!!!!!
引用文獻
springboot項目的優雅關閉方式:https://blog.csdn.net/XlxfyzsFdblj/article/details/82054744
環境
Eclipse+Chrome+Postman(網絡請求調試的Chrome插件)
1.開啟運行Application測試類【主流/推薦】
package com.edu.xhu.jobspider.examples.controller; /** * SpringBoot Web測試運行類 * @author Johnny Zen * @createTime 2018年9月24日 * @description 創建步驟: * 1.先創建maven web項目 * 2.繼承parent,增加springboot-web依賴,配置打包標簽 * 3.配置application.yml * 4.創建運行文件App.java * 5.用java -jar 文件.jar 進行運行 */ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @EnableAutoConfiguration //@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @RestController @RequestMapping("/demo01") public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } @RequestMapping("/hello") public String hello(){ return "hello-SpringBootApp!!"; } }
Step1:Eclipse:[App.Java]>右鍵>Run As>Java Application
Step2:Chrome:http://localhost:8080/demo/demo01/hello
2.安全關閉Application
【不推薦方法】關閉正在開發環境的IDE(Eg:Eclipse)
【推薦方法】
Step1:Maven中添加依賴項
<!-- actuator(為關閉application) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
Step2:Application.yml增加配置
# 關閉Application endpoints: shutdown: enabled: true #啟用shutdown sensitive: false #禁用密碼驗證
Step3:Postman 運行shutdown命令請求:http://localhost:8080/demo/shutdown