6、异常报告器解析
public ConfigurableApplicationContext run(String... args) { Collection<springbootexceptionreporter> exceptionReporters = new ArrayList<>();
// 删去了部分代码,在调用run方法时会初始化异常报告器集合
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
return context;
}
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));
// 也是通过SpringFactoriesLoader技术来加载,见下图,默认就一个,这边实例化会调用其构造函数
List<t> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
AnnotationAwareOrderComparator.sort(instances);
return instances;
}
FailureAnalyzers(ConfigurableApplicationContext context, ClassLoader classLoader) { this.classLoader = (classLoader != null) ? classLoader : context.getClassLoader();
// 获取容器中FailureAnalyzer实例对象并排序
this.analyzers = loadFailureAnalyzers(this.classLoader);
prepareFailureAnalyzers(this.analyzers, context);
}
private List<failureanalyzer> loadFailureAnalyzers(ClassLoader classLoader) { 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());
}
}
AnnotationAwareOrderComparator.sort(analyzers);
return analyzers;
}
private void prepareFailureAnalyzers(List<failureanalyzer> analyzers, ConfigurableApplicationContext context) { for (FailureAnalyzer analyzer : analyzers) {
prepareAnalyzer(context, analyzer);
}
}
private void prepareAnalyzer(ConfigurableApplicationContext context, FailureAnalyzer analyzer) {
if (analyzer instanceof BeanFactoryAware) {
((BeanFactoryAware) analyzer).setBeanFactory(context.getBeanFactory());
}
if (analyzer instanceof EnvironmentAware) {
((EnvironmentAware) analyzer).setEnvironment(context.getEnvironment());
}
}
```</failureanalyzer></failureanalyzer></string></failureanalyzer></t></string></t></t></t></springbootexceptionreporter>
以上是 6、异常报告器解析 的全部内容, 来源链接: utcz.com/z/513559.html