Spring源码

Spring源码7. SpringBootExceptionRe

2019-07-31  本文已影响0人  ygxing

上篇回顾

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<FailureAnalyzer> 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<FailureAnalyzer> 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<String> analyzerNames = SpringFactoriesLoader
                .loadFactoryNames(FailureAnalyzer.class, classLoader);
        List<FailureAnalyzer> 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()准备应用上下文

上一篇下一篇

猜你喜欢

热点阅读