亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

SpringBoot之自定義啟動異常堆棧信息打印方式

瀏覽:97日期:2023-02-18 16:10:12

在SpringBoot項目啟動過程中,當一些配置或者其他錯誤信息會有一些的規范的提示信息

***************************APPLICATION FAILED TO START***************************

Description:

Web server failed to start. Port 8080 was already in use.

Action:

Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port.

在SpringBoot 中其實現原理是什么,我們該如何自定義異常信息呢

1、SpringBoot異常處理的源碼分析

在springboot啟動的核心方法run中會加載所有的SpringBootExceptionReporter

exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context);

調用了getSpringFactoriesInstances方法

private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) { ClassLoader classLoader = getClassLoader(); // Use names and ensure unique to protect against duplicates Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader)); List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names); AnnotationAwareOrderComparator.sort(instances); return instances; }

其主要通過Spring的Factories機制來加載

public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); configureHeadlessProperty(); SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); Banner printedBanner = printBanner(environment); context = createApplicationContext(); exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); prepareContext(context, environment, listeners, applicationArguments, printedBanner); refreshContext(context); afterRefresh(context, applicationArguments); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); } listeners.started(context); callRunners(context, applicationArguments); } catch (Throwable ex) { //異常捕獲中,向用戶打印異常信息 handleRunFailure(context, ex, exceptionReporters, listeners); throw new IllegalStateException(ex); } try { listeners.running(context); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, null); throw new IllegalStateException(ex); } return context; }

在try catch中,catch會打印異常信息

private void handleRunFailure(ConfigurableApplicationContext context, Throwable exception, Collection<SpringBootExceptionReporter> exceptionReporters, SpringApplicationRunListeners listeners) { try { try { handleExitCode(context, exception); if (listeners != null) { listeners.failed(context, exception); } } finally { reportFailure(exceptionReporters, exception); if (context != null) { context.close(); } } } catch (Exception ex) { logger.warn('Unable to close ApplicationContext', ex); } ReflectionUtils.rethrowRuntimeException(exception); }

private void reportFailure(Collection<SpringBootExceptionReporter> exceptionReporters, Throwable failure) { try { for (SpringBootExceptionReporter reporter : exceptionReporters) { if (reporter.reportException(failure)) { registerLoggedException(failure); return; } } } catch (Throwable ex) { // Continue with normal handling of the original failure } if (logger.isErrorEnabled()) { logger.error('Application run failed', failure); registerLoggedException(failure); } }

遍歷exceptionReporters,打印日常信息

SpringBoot之自定義啟動異常堆棧信息打印方式

SpringBootExceptionReporter是一個回調接口,用于支持對SpringApplication啟動錯誤的自定義報告。里面就一個報告啟動失敗的方法。

其實現類:org.springframework.boot.diagnostics.FailureAnalyzers

用于觸發從spring.factories加載的FailureAnalyzer和FailureAnalysisReporter實例。

2、如何自定義異常信息

/** * <p> * * <p> * * @author: xuwd * @time: 2020/11/16 10:52 */public class WannaStopException extends RuntimeException {}

自定義異常信息打印

public class StopFailureAnalyzerextends AbstractFailureAnalyzer<WannaStopException> { @Override protected FailureAnalysis analyze(Throwable rootFailure, WannaStopException cause) {for (StackTraceElement stackTraceElement : cause.getStackTrace()) { if (stackTraceElement.getClassName().equals('com.pigx.demo.Config21')) {return new FailureAnalysis('A想停止', '別要A了', cause); }}return null; }}

接下來令他生效,通過上面分析可以可看出需要通過AutoConfigurationImportSelector,類似于自定義SpringBoot Starter AutoConfiguration的形式,我們需要在META-INF/spring.factories文件內進行定義,如下所示:

SpringBoot之自定義啟動異常堆棧信息打印方式

接著在合適的地方拋出WannaStopException 異常

總結

在springboot 啟動過程中會先對異常信息進行補捕獲,對進行日志格式處理的日志進行處理;其核心是通過SpringBootExceptionReporter回調及sping-spi bean的管理。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 99欧美在线| 国产91色综合久久免费分享 | 日韩亚洲一区二区三区 | 久久精品国产999大香线焦 | 天堂久久久久久中文字幕 | 麻豆国产精品入口免费观看 | 美国毛片在线 | 亚洲一区二区三区在线 | 在线观看欧美大片 | www.精品在线 | 亚洲综合网在线 | 日韩在线观看一区二区不卡视频 | 男人狂躁女人下面视频免费观看 | www.久久爱.com狼人 | 国产成人精品日本亚洲直接 | 特级av毛片免费观看 | 国产成人亚洲综合在线 | 国产乱人乱精一区二区视频密 | 国产色啪午夜免费视频 | 一级片在线免费观看 | 国外欧美一区另类中文字幕 | 中文字幕日韩一区二区三区不卡 | 久久久久国产精品免费免费 | 日韩版码免费福利视频 | 成人久久网| 第一次男生很猛的进去视频 | 中文第一页 | 国产成人aaa在线视频免费观看 | 国产色片在线观看 | 亚洲综合在线观看视频 | 成人国产精品 | 日韩视频在线观看 | 欧美 亚洲 国产 精品有声 | 又爽又黄又无遮挡的视频美女软件 | 国产成人免费观看在线视频 | 国产精品久久久久久免费播放 | 精品国产一区二区三区不卡在线 | 成人在线免费视频播放 | 国产精品理论片在线观看 | 久久精品大全 | 美女喷水视频在线观看 |