Spring源码7. SpringBootExceptionReporter异常上报

上篇回顾

createApplicationContext()创建应用上下文

获取并打印Spring启动过程中的异常信息

//这里从META-INF/spring.factories中获取并初始化SpringBootExceptionReporter子类列表
//获取到一个FailureAnalyzers实例
exceptionReporters = getSpringFactoriesInstances(
                SpringBootExceptionReporter.class,
                new Class[] { ConfigurableApplicationContext.class }, context);

FailureAnalyzers类源码

//异常失败原因分析器
final class FailureAnalyzers implements SpringBootExceptionReporter {

    //失败分析器
    private final List analyzers;

    //构造函数
    FailureAnalyzers(ConfigurableApplicationContext context, ClassLoader classLoader) {
        Assert.notNull(context, "Context must not be null");
        this.classLoader = (classLoader != null) ? classLoader : context.getClassLoader();
        //加载失败分析器列表
        this.analyzers = loadFailureAnalyzers(this.classLoader);
        //准备分析异常原因
        prepareFailureAnalyzers(this.analyzers, context);
    }

    //加载异常分析器
    private List loadFailureAnalyzers(ClassLoader classLoader) {
       //通过META-INF/spring.factories中,获取到FailureAnalyzer的子类列表如下
        //BeanCurrentlyInCreationFailureAnalyzer,
        //BeanDefinitionOverrideFailureAnalyzer,
        //BeanNotOfRequiredTypeFailureAnalyzer,
        //BindFailureAnalyzer,
        //BindValidationFailureAnalyzer,
        //UnboundConfigurationPropertyFailureAnalyzer,
        //ConnectorStartFailureAnalyzer,
        //NoSuchMethodFailureAnalyzer,
        //NoUniqueBeanDefinitionFailureAnalyzer,
        //PortInUseFailureAnalyzer,
        //ValidationExceptionFailureAnalyzer,
        //InvalidConfigurationPropertyNameFailureAnalyzer,
        //InvalidConfigurationPropertyValueFailureAnalyzer,
        //NoSuchBeanDefinitionFailureAnalyzer,
        //DataSourceBeanCreationFailureAnalyzer,
        //ikariDriverConfigurationFailureAnalyzer,
        //NonUniqueSessionRepositoryFailureAnalyzer
        List analyzerNames = SpringFactoriesLoader
                .loadFactoryNames(FailureAnalyzer.class, classLoader);
        List analyzers = new ArrayList<>();
        for (String analyzerName : analyzerNames) {
            try {
                Constructor constructor = ClassUtils.forName(analyzerName, classLoader)
                        .getDeclaredConstructor();
                ReflectionUtils.makeAccessible(constructor);
                analyzers.add((FailureAnalyzer) constructor.newInstance());
            }
            catch (Throwable ex) {
                logger.trace("Failed to load " + analyzerName, ex);
            }
        }
        AnnotationAwareOrderComparator.sort(analyzers);
        return analyzers;
    }
}

下一篇

prepareContext()准备应用上下文

你可能感兴趣的:(Spring源码7. SpringBootExceptionReporter异常上报)