出口代码映射

出口代码的默认行为如下:spring-doc.cadn.net.cn

指挥注册可以自定义异常退出代码之间的映射。Spring shell 采用了类似的方法Spring靴关于退出代码,简单来说整合进去。spring-doc.cadn.net.cn

假设下面显示的异常会从命令中抛出:spring-doc.cadn.net.cn

static class MyException extends RuntimeException {

	private final int code;

	MyException(String msg, int code) {
		super(msg);
		this.code = code;
	}

	public int getCode() {
		return code;
	}
}

可以定义一个映射函数可投掷还有退出代码。你也可以直接配置类去退出代码,这只是配置中的一个语法糖。spring-doc.cadn.net.cn

CommandRegistration.builder()
	.withExitCode()
		.map(MyException.class, 3)
		.map(t -> {
			if (t instanceof MyException) {
				return ((MyException) t).getCode();
			}
			return 0;
		})
		.and()
	.build();
出口代码无法通过基于注释的配置进行自定义