##2.1.4.RELEASE版本源代码做为参考
SpringApplication 对象的 run 方法的源码和运行流程。
public static void main(String[] args) {
SpringApplication.run(BjsdzkApiApplication.class, args);
// http://localhost:63336/api/doc.html
// http://localhost:63336/api/swagger-ui.html
}
进入run方法
public static ConfigurableApplicationContext run(Class<?>[] primarySources,
String[] args) {
return new SpringApplication(primarySources).run(args);
}
这里开始初始化,查看SpringApplication.class构造函数:
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
//设置servlet环境
this.webApplicationType = WebApplicationType.deduceFromClasspath();
//获取ApplicationContextInitializer,也是在这里开始首次加载spring.factories文件
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
//获取监听器,这里是第二次加载spring.factories文件
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
查看断点信息:
标记几个重点方法:
this.webApplicationType = WebApplicationType.deduceFromClasspath();
这一步设计环境,最终类型为:SERVLET
下一步:
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));这一步获取ApplicationContextInitializer,也是在这里开始首次加载spring.factories文件,主要是加载容器初始化需要的类(ApplicationContextInitializer是spring组件spring-context组件中的一个接口,主要是spring ioc容器刷新之前的一个回调接口,用于处于自定义逻辑)
内部声明ApplicationListener.class,进入查看源码:声明了触发的事件
@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
void onApplicationEvent(E var1);
}
再看getSpringFactoriesInstances方法,分了俩步,一步加载(loadSpringFactories),一步创建(createSpringFactoriesInstances)
查看代码
private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
ClassLoader classLoader = this.getClassLoader();
Set<String> names = new LinkedHashSet(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
List<T> instances = this.createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
AnnotationAwareOrderComparator.sort(instances);
return instances;
}
进入loadSpringFactories方法
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
......
while(urls.hasMoreElements()) {
URL url = (URL)urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
Iterator var6 = properties.entrySet().iterator();
.....
}
URL url = (URL)urls.nextElement();
UrlResource resource = new UrlResource(url);便是加载META-INF/spring.factories 的url(class)
回头再看下createSpringFactoriesInstances方法
private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, ClassLoader classLoader, Object[] args, Set<String> names) {
List<T> instances = new ArrayList(names.size());
Iterator var7 = names.iterator();
while(var7.hasNext()) {
String name = (String)var7.next();
try {
Class<?> instanceClass = ClassUtils.forName(name, classLoader);
Assert.isAssignable(type, instanceClass);
Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes);
T instance = BeanUtils.instantiateClass(constructor, args);
instances.add(instance);
} catch (Throwable var12) {
throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, var12);
}
}
return instances;
}
拿到url初始化器实例并返回
好的,这里我们回头到初始化方法里的倒数第二步this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class))方法,过程同上,该方法获取ApplicationListener.class监听器,这里是第二次加载spring.factories文件
最终加载了13个Listener
最后一步终:返回入口应用类
private Class<?> deduceMainApplicationClass() {
try {
StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
if ("main".equals(stackTraceElement.getMethodName())) {
return Class.forName(stackTraceElement.getClassName());
}
}
}
catch (ClassNotFoundException ex) {
// Swallow and continue
}
return null;
}
StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();返回表示此线程的堆栈转储的堆栈跟踪元素数组。