> For the complete documentation index, see [llms.txt](https://298073941.gitbook.io/memo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://298073941.gitbook.io/memo/spring-boot/springapplication.md).

# SpringApplication

#### 点开SpringApplication源码会发现开头注解已经介绍了这个类的作用，就是加载spring应用bootstrap。

#### 具体步骤

1. 根据你的classpath创建一个适当的spring实例(web,none)
2. 注册一个commandlinepropertysource，暴露一个命令行给spring配置
3. 刷新context上下文
4. 触发后置的一些bean，如commandlinerunner

#### 构造器分析

构造期参数有两个，一个为resourceloader，一个为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));//资源路径，配合默认加载器使用
   this.webApplicationType = WebApplicationType.deduceFromClasspath();//判断web容器类型
   setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));//设置pre context 的初始化工具类
   setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));//设置监听器   
   this.mainApplicationClass = deduceMainApplicationClass();//寻找main的类
}
```

```
	private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
		ClassLoader classLoader = getClassLoader();
		// Use names and ensure unique to protect against duplicates
		Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
		List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
		AnnotationAwareOrderComparator.sort(instances);
		return instances;
	}
```

* WebApplicationType 一般为servlet，内部实现通过catch class.forName 是否能找到对应type类
* 先看getSpringFacotryiesInstances方法，看名字大概也能猜到就是利用spi机制创建springboot的基础配置，位置就在META-INF/spring.factories
* mainApplicationClass利用创建new RuntimeException 获取StackTrace 然后迭代栈判断方法名是否为main
*

````
```
````

````
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
org.springframework.boot.context.ContextIdApplicationContextInitializer,\
org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\
org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer,\
org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer

org.springframework.context.ApplicationListener=\
org.springframework.boot.ClearCachesApplicationListener,\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.ConfigFileApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\
org.springframework.boot.context.logging.LoggingApplicationListener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener
```
````

#### Run

```
	public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
		configureHeadlessProperty();
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.starting();
		try {
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
			ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
			configureIgnoreBeanInfo(environment);
			Banner printedBanner = printBanner(environment);
			context = createApplicationContext();
			exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
					new Class[] { ConfigurableApplicationContext.class }, context);
			prepareContext(context, environment, listeners, applicationArguments, printedBanner);
			refreshContext(context);
			afterRefresh(context, applicationArguments);
			stopWatch.stop();
			if (this.logStartupInfo) {
				new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
			}
			listeners.started(context);
			callRunners(context, applicationArguments);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, exceptionReporters, listeners);
			throw new IllegalStateException(ex);
		}

		try {
			listeners.running(context);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, exceptionReporters, null);
			throw new IllegalStateException(ex);
		}
		return context;
	}
```

1. 计时器启动
2. configureHeadlessProperty不用关心
3. 获取SpringApplicationRunListeners，内部实现和上面实例application差不多
4. prepareEnviroment
5. 下面不写了 累了 下次补充
