- 以启动类为入口
@SpringBootApplication
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
- run方法进来会new一个SpringApplication
public static ConfigurableApplicationContext run(Class<?>[] primarySources,
String[] args) {
return new SpringApplication(primarySources).run(args);
}
// 接上面方法
public SpringApplication(Class<?>... primarySources) {
this(null, primarySources);
}
// 接上面方法
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
//将primarySources转为LinkedHashSet
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
//推断是否为web应用(具体逻辑)
this.webApplicationType = WebApplicationType.deduceFromClasspath();
//找到类路径下的META-INF/spring.factories下的所有ApplicationContextInitializer,并保存起来
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
//找到类路径下的META-INF/spring.factories下的所有ApplicationListener,并保存起来
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
//找到含有main方法的主类
this.mainApplicationClass = deduceMainApplicationClass();
}
网友评论