Spring Boot的exit code
Spring Boot的exit code
任何应用程序都有exit code,这个code是int值包含负值,在本文中我们将会探讨Spring Boot中的 exit code。
Spring Boot的exit code
Spring Boot如果启动遇到错误,则会返回1.正常退出的话则会返回0.
Spring Boot向JVM注册了shutdown hooks来保证应用程序优雅的退出。Spring Boot还提供了org.springframework.boot.ExitCodeGenerator接口,来方便自定义退出code.
自定义Exit Codes
Spring Boot提供了三种方式来让我们自定义exit code。
ExitCodeGenerator,ExitCodeExceptionMapper和ExitCodeEvent。下面我们分别来讲解。
ExitCodeGenerator
实现ExitCodeGenerator接口,我们需要自己实现getExitCode()方法来自定义返回代码:
@SpringBootApplication
public class ExitCodeApp implements ExitCodeGenerator {
public static void main(String[] args) {
System.exit(SpringApplication.exit(SpringApplication.run(ExitCodeApp.class, args)));
}
@Override
public int getExitCode() {
return 11;
}
}
这里我们调用了System.exit方法来返回特定的代码。
ExitCodeExceptionMapper
如果我们遇到runtime exception的时候,可以使用ExitCodeExceptionMapper来做错误代码的映射如下:
@Bean
CommandLineRunner createException() {
return args -> Integer.parseInt("test") ;
}
@Bean
ExitCodeExceptionMapper exitCodeToExceptionMapper() {
return exception -> {
// set exit code base on the exception type
if (exception.getCause() instanceof NumberFormatException) {
return 80;
}
return 1;
};
}
上面的例子我们创建了一个CommandLineRunner bean,在实例化的过程中会抛出NumberFormatException,然后在ExitCodeExceptionMapper中,我们会捕捉到这个异常,返回特定的返回值。
ExitCodeEvent
我们还可以使用ExitCodeEvent来捕捉异常事件如下所示:
@Bean
DemoListener demoListenerBean() {
return new DemoListener();
}
private static class DemoListener {
@EventListener
public void exitEvent(ExitCodeEvent event) {
System.out.println("Exit code: " + event.getExitCode());
}
}
当应用程序退出的时候,exitEvent() 方法会被调用。
本文的例子可以参考:https://github.com/ddean2009/learn-springboot2/tree/master/springboot-exitcode
更多教程请参考 flydean的博客
Spring Boot的exit code的更多相关文章
- Ubuntu下使用VS Code创建Spring Boot工程
目的 我们将在Ubuntu桌面系统下,使用VS Code(Visual Studio Code)编辑器从零开始创建一个Spring Boot工程,并实现一个简单的RESTful风格接口.使用这套流程的 ...
- Spring Boot Reference Guide
Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch, ...
- spring boot 运行提示:Process finished with exit code 1
spring boot 运行提示:Process finished with exit code 1 经检查发现是由于在application.properties配置文件中将某些自定义配置项移除了, ...
- VS Code打开使用IDEA搭建的Spring Boot项目运行提示"snakeyaml was not found on the classpath"错误
今天用VS Code打开之前基于IDEA搭建并开发的Spring Boot项目,启动调试后出现如下错误: 17:43:05.214 [restartedMain] ERROR org.springfr ...
- 基于VS Code创建Spring Boot项目开发REST API(一)
公司从.NET转向Java不仅仅是简单的代码变成Java,趁此机会对原有的架构和代码重构,融入新的概念和技术.目前通过前后端分离,将后端更多的微服务化.从.NET转向Java我们更多的是用Java开发 ...
- vs code 配置spring boot开发环境
一.环境变量 jdk环境变量一键设置 管理員运行 - 一支小白 - 博客园https://www.cnblogs.com/startnow/p/7416533.html 二.安装插件 1.Java E ...
- 安装xcode6 beta 后调试出现Unable to boot the iOS Simulator以及编译苹果官方Swift的demo报错failed with exit code 1的解决的方法
苹果昨天公布新语言Swift(雨燕),须要安装xcode6 以及mac os 系统为10.9以上. (xcode6 beta 可在官方下载.须要登录开发人员账号:mac os 系统直接更新就可以.在此 ...
- 如何使用VS Code编写Spring Boot (第二弹)
本篇文章是续<如何使用VS Code编写Spring Boot> 之后,结合自己.net经验捣鼓的小demo,一个简单的CRUD,对于习惯了VS操作模式的.net人员非常方便,强大的智能提 ...
- Spring Boot文档阅读
原因之初 最初习惯百度各种博客教程,然后跟着操作,因为觉得跟着别人走过的路走可以少走很多弯路,省时间.然而,很多博客的内容并不够完整,甚至错误,看多了的博客甚至有千篇一律的感觉.此外,博客毕竟是记载博 ...
随机推荐
- linux 之虚拟机的安装与介绍
linux 零基础入门1.1linux介绍 操作系统用途: 管理硬件 驱动硬件 管理软件 分配资源1.2 linux的发展unix -> windows ->linuxlinux 免费 开 ...
- 监控一姐Grafana你可认识?
我们先来认识一下格拉法纳——Grafana. 我去,这不就是实时监控大屏吗?记得 N 年前,部门为了做这么个功能,还花重金请专业公司搞过类似的图,现在想想其实也很简单呀. 话又说回来,其实 Grafa ...
- Pytest系列(8) - 使用自定义标记mark
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 pytest 可以支持自定义 ...
- react axios 跨域访问一个或多个域名
1.react + axios 跨域访问一个域名 配置非常简单,只需要在当前的 package.json 文件里面配置: "proxy":"http://iot-demo ...
- 本地代码上传到git仓库(github)
准备:拥有自己的github账号:电脑上安装了git; 1.进入github,进入仓库点击NEW(新建仓库) 2.新建仓库 Repository name :仓库名称: Description (op ...
- 搭建DVWA Web渗透测试靶场
文章更新于:2020-04-13 按照惯例,需要的文件附上链接放在文首. 文件名:DVWA-1.9-2020.zip 文件大小:1.3 M 文件说明:这个是新版 v1.9 (其实是 v1.10开发版) ...
- Altium Designer 3D
- [转] [知乎] 浅谈Roguelike
浅谈Roguelike 从柏林诠释说起 在2008年召开的国际Roguelike开发会议上,众多的Roguelike开发者与爱好者共同制定了<柏林诠释>,规定了Roguelike游戏需要具 ...
- three.js obj转js的详细步骤 convert_obj_three.py的用法
three.js是最近非常流行的一个前端webgl库. js格式的模型文件是three.js中可以直接加载的文件.使用THREE.JSONLoader()直接加载,而不需要引用其它的loader插件. ...
- I - Red and Black DFS
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...