- run方法启动时传入了当前类 ContractApplication.class,传入当前类的作用主要是为了解析当前类上面的注解(不一定传递当前类,也可以自己写一个类,在写的类上添加对应的注解)
@SpringBootApplication
@ComponentScan("com.example")
public class ContractApplication {
public static void main(String[] args) {
SpringApplication.run(ContractApplication.class);
}
}
- 将传入的类封装成一个Class类型的数组
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
return run(new Class[]{primarySource}, args);
}
- 生成一个SpringApplication对象,在调用SpringApplication的run方法
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
return (new SpringApplication(primarySources)).run(args);
}
- new SpringApplication()构造方法,主要是给SpringApplication对象中的属性赋值
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
//资源加载器
this.resourceLoader = resourceLoader;
//判断传进来的类是否为空
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
//1.推测项目的应用类型(通过项目目录结构,和类),NONE(不是Web项目),REACTIVE(Web应用,响应式),SERVLET(Web应用,springmvc)
this.webApplicationType = WebApplicationType.deduceFromClasspath();
//2.从spring.factories中获取需要加载的BootstrapRegistryInitializer类型的的实现类
this.bootstrapRegistryInitializers = new ArrayList<>(
getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
//3.从spring.factories中获取需要加载的AppliationContextInitializer(初始化器)类型的实现类
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
//4.从spring.factories中获取需要加载的ApplicationListener(应用监听器)类型的实现类
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
//5.推测出Main类(main()方法所在的类)
this.mainApplicationClass = deduceMainApplicationClass();
}
- spring.factories
# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer
标签:run,SpringBoot,启动,流程,springframework,primarySources,Class,SpringApplication,clas
From: https://www.cnblogs.com/blackyoumo/p/16940591.html