完成初始化和優雅地退出Spring boot 服務的啓動類

啓動類

@SpringBootApplication
@CrossOrigin
@Controller
public class My3dApplication implements CommandLineRunner, ApplicationContextAware {
	@Value("${web.upload.models}")
	private String models;
	
	private ApplicationContext context;

	/**
	 * 完成初始化環境目錄
	 * 
	 * @param args
	 * @throws Exception
	 */
	@Override
	public void run(String... args) throws Exception {
		// 獲取jar運行當前工作目錄
		String root = System.getProperty("user.dir") + System.getProperty("file.separator") ;
		// 在此完成初始化內容
		// ...
	}

	public static void main(String[] args) {
		SpringApplication.run(My3dApplication.class, args);
	}

	@GetMapping(value = { "/", "/#", "/index", "/home" })
	public String index() {
		return "index.html";
	}

	// 優雅地關閉當前服務
	@PostMapping(value = "/shutdown")
	public void shutdownContext() {
		((ConfigurableApplicationContext) context).close();
	}

	// 獲取當前服務對象
	@Override
	public void setApplicationContext(ApplicationContext ctx) throws BeansException {
		this.context = ctx;
	}
}

在前端關閉Spring Boot服務的Javascript

function shutdown() {
	if(confirm(" 您確定要退出本服務?")) {
		$.ajax({
			url : '/shutdown',
			type : 'post', 
			dataType : 'text',
			success: null
		});
		setTimeout(function() {  //使用  setTimeout()方法設定定時2000毫秒
			window.location.reload();//頁面刷新
		}, 1000);
	}
	return true;
}
發佈了30 篇原創文章 · 獲贊 2 · 訪問量 4039
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章