springboot准备阶段:
举例入口SpringApplication.run(DiveInSBApp.class);
run(Class<?> primarySource, String... args)-->
run(new Class<?>[] { primarySource }, args)-->
SpringApplication(primarySources).run(args)
1.String… 可变参简化了方法重载
2.primarySource参数变化 1->[],实现类未数组形式,但提供了便利的入口
3.将初始化放在构造函数中
public SpringApplication(Class<?>... primarySources) {
this(null, primarySources);
}
//以下未实现类
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();
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
1.调用时在判空Assert.notNull()
2.WebApplicationType.deduceFromClasspath();
推断应用类型。枚举类中实现推断方法,很赞可以项目中用到
3.
对应spring.factories
,主要实现了初始化和监听
查看ApplicationListener,发现这是spring的接口并继承事件监听的顶级父类EventListener;
阅读源码时遇到了transient关键字,作用是避免被序列化;@FunctionalInterface
表试继承该接口的类可以实现函数话编程(Java8新特性)。