@SpringBootApplication
public class Application {
public static void main(String[] args) {
/**
* run(Class<?> primarySource,String... args)
* @param primarySource 主要加载源
* @param args 应用程序参数
* @return the running {@link ApplicationContext}
*/
ApplicationContext run = SpringApplication.run(Application.class, args);
System.out.println(run.toString());
}
}
一般来说,SpringBoot启动类如上图所示。
一、启动当前类,然后调用run方法,调用重载run方法创建SpringApplication();
SpringApplication创建传入当前运行类,
- 配置resourceLoader(资源加载器)
- 设置主要Bean的来源
- 判断当前应用程序的类型(REACTIVE,NONE,SEVLET)
- 获取Spring的工厂实例对象
- 获取监听器的实例对象
- 获取堆栈信息,找到main程序,开始执行
/** * Create a new {@link SpringApplication} instance. The application context will load * beans from the specified primary sources (see {@link SpringApplication class-level} * documentation for details. The instance can be customized before calling * {@link #run(String...)}. * @param resourceLoader the resource loader to use * @param primarySources the primary bean sources * @see #run(Class, String[]) * @see #setSources(Set) */ @SuppressWarnings({ "unchecked", "rawtypes" }) 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(); }
二、run启动过程
- 设置启动时的时间
- 设置应用上下文 null
- 设置异常报告器
- 设置 java.awt.headless系统参数为true
先这样明天再写