SpringBoot中Application开启与关闭

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

3.推荐文献

springboot 2.0.4 关闭程序————我走过的那些坑 - CSDN

原文地址:https://www.cnblogs.com/johnnyzen/p/9696614.html