SpringApplication
点开SpringApplication源码会发现开头注解已经介绍了这个类的作用,就是加载spring应用bootstrap。
具体步骤
构造器分析
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;//资源加载器,为空的时候会使用默认加载器
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));//资源路径,配合默认加载器使用
this.webApplicationType = WebApplicationType.deduceFromClasspath();//判断web容器类型
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));//设置pre context 的初始化工具类
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));//设置监听器
this.mainApplicationClass = deduceMainApplicationClass();//寻找main的类
} 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;
}Run
Last updated