1. 程式人生 > 程式設計 >SpringBoot列印啟動時異常堆疊資訊詳解

SpringBoot列印啟動時異常堆疊資訊詳解

SpringBoot在專案啟動時如果遇到異常並不能友好的打印出具體的堆疊錯誤資訊,我們只能檢視到簡單的錯誤訊息,以致於並不能及時解決發生的問題,針對這個問題SpringBoot提供了故障分析儀的概念(failure-analyzer),內部根據不同型別的異常提供了一些實現,我們如果想自定義該怎麼去做?

FailureAnalyzer

SpringBoot提供了啟動異常分析介面FailureAnalyzer,該介面位於org.springframework.boot.diagnosticspackage內。
內部僅提供一個分析的方法,原始碼如下所示:

@FunctionalInterface
public interface FailureAnalyzer {

 /**
  * Returns an analysis of the given {@code failure},or {@code null} if no analysis
  * was possible.
  * @param failure the failure
  * @return the analysis or {@code null}
  */
 FailureAnalysis analyze(Throwable failure);

}

該介面會把遇到的異常物件例項Throwable failure交付給實現類,實現類進行自定義處理。

AbstractFailureAnalyzer

AbstractFailureAnalyzer是FailureAnalyzer的基礎實現抽象類,實現了FailureAnalyzer定義的analyze(Throwable failure)方法,並提供了一個指定異常型別的抽象方法analyze(Throwable rootFailure,T cause),原始碼如下所示:

public abstract class AbstractFailureAnalyzer<T extends Throwable> implements FailureAnalyzer {

 @Override
 public FailureAnalysis analyze(Throwable failure) {
  T cause = findCause(failure,getCauseType());
  if (cause != null) {
   return analyze(failure,cause);
  }
  return null;
 }

 /**
  * Returns an analysis of the given {@code rootFailure},or {@code null} if no
  * analysis was possible.
  * @param rootFailure the root failure passed to the analyzer
  * @param cause the actual found cause
  * @return the analysis or {@code null}
  */
 protected abstract FailureAnalysis analyze(Throwable rootFailure,T cause);

 /**
  * Return the cause type being handled by the analyzer. By default the class generic
  * is used.
  * @return the cause type
  */
 @SuppressWarnings("unchecked")
 protected Class<? extends T> getCauseType() {
  return (Class<? extends T>) ResolvableType.forClass(AbstractFailureAnalyzer.class,getClass()).resolveGeneric();
 }

 @SuppressWarnings("unchecked")
 protected final <E extends Throwable> E findCause(Throwable failure,Class<E> type) {
  while (failure != null) {
   if (type.isInstance(failure)) {
    return (E) failure;
   }
   failure = failure.getCause();
  }
  return null;
 }

}

通過AbstractFailureAnalyzer原始碼我們可以看到,它在實現於FailureAnalyzer的介面方法內進行了特殊處理,根據getCauseType()方法獲取當前類定義的第一個泛型型別,也就是我們需要分析的指定異常型別。

獲取泛型異常型別後根據方法findCause判斷Throwable是否與泛型異常型別匹配,如果匹配直接返回給SpringBoot進行註冊處理。

SpringBoot提供的分析實現

SpringBoot內部通過實現AbstractFailureAnalyzer抽象類定義了一系列的針對性異常型別的啟動分析,如下圖所示:

指定異常分析

SpringBoot內部提供的啟動異常分析都是指定具體的異常型別實現的,最常見的一個錯誤就是埠號被佔用(PortInUseException),雖然SpringBoot內部提供一個這個異常的啟動分析,我們也是可以進行替換這一異常分析的,我們只需要建立PortInUseException異常的AbstractFailureAnalyzer,並且實現類註冊給SpringBoot即可,實現自定義如下所示:

/**
 * 埠號被佔用{@link PortInUseException}異常啟動分析
 *
 * @author 恆宇少年
 */
public class PortInUseFailureAnalyzer extends AbstractFailureAnalyzer<PortInUseException> {
 /**
  * logger instance
  */
 static Logger logger = LoggerFactory.getLogger(PortInUseFailureAnalyzer.class);

 @Override
 protected FailureAnalysis analyze(Throwable rootFailure,PortInUseException cause) {
  logger.error("埠被佔用。",cause);
  return new FailureAnalysis("埠號:" + cause.getPort() + "被佔用","PortInUseException",rootFailure);
 }
}

註冊啟動異常分析

在上面我們只是編寫了指定異常啟動分析,我們接下來需要讓它生效,這個生效方式比較特殊,類似於自定義SpringBoot Starter AutoConfiguration的形式,我們需要在META-INF/spring.factories檔案內進行定義,如下所示:

org.springframework.boot.diagnostics.FailureAnalyzer=\
 org.minbox.chapter.springboot.failure.analyzer.PortInUseFailureAnalyzer

那我們為什麼需要使用這種方式定義呢?

專案啟動遇到的異常順序不能確定,很可能在Spring IOC並未執行初始化之前就出現了異常,我們不能通過@Component註解的形式使其生效,所以SpringBoot提供了通過spring.factories配置檔案的方式定義。

啟動異常分析繼承關係

自定義的執行異常一般都是繼承自RuntimeException,如果我們定義一個RuntimeException的異常啟動分析例項會是什麼效果呢?

/**
 * 專案啟動執行時異常{@link RuntimeException}統一啟動分析
 *
 * @author 恆宇少年
 */
public class ProjectBootUnifiedFailureAnalyzer extends AbstractFailureAnalyzer<RuntimeException> {
 /**
  * logger instance
  */
 static Logger logger = LoggerFactory.getLogger(ProjectBootUnifiedFailureAnalyzer.class);

 @Override
 protected FailureAnalysis analyze(Throwable rootFailure,RuntimeException cause) {
  logger.error("遇到執行時異常",cause);
  return new FailureAnalysis(cause.getMessage(),"error",rootFailure);
 }
}

將該類也一併註冊到spring.factories檔案內,如下所示:

org.springframework.boot.diagnostics.FailureAnalyzer=\
 org.minbox.chapter.springboot.failure.analyzer.PortInUseFailureAnalyzer,\
 org.minbox.chapter.springboot.failure.analyzer.ProjectBootUnifiedFailureAnalyzer

執行專案並測試埠號被佔用異常我們會發現,並沒有執行ProjectBootUnifiedFailureAnalyzer內的analyze方法,而是繼續執行了PortInUseFailureAnalyzer類內的方法。

那我們將PortInUseFailureAnalyzer這個啟動分析從spring.factories檔案內暫時刪除掉,再來執行專案我們會發現這時卻是會執行ProjectBootUnifiedFailureAnalyzer類內分析方法。

總結

根據本章我們瞭解了SpringBoot提供的啟動異常分析介面以及基本抽象實現類的運作原理,而且啟動異常分析存在分析泛型異常類的上下級繼承關係,異常子類的啟動分析會覆蓋掉異常父類的啟動分析,如果你想包含全部異常的啟動分析可以嘗試使用Exception作為AbstractFailureAnalyzer的泛型引數。

以上就是本次介紹的全部知識點內容,希望我們整理的內容能夠幫助到大家。