springboot 源碼SpringApplication的run方法解析

public ConfigurableApplicationContext run(String... args) {
//記錄啓動應用啓動時間
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
//構建HeadlessProperty 默認設置java.awt.headless屬性爲true
		configureHeadlessProperty();
//構建SpringApplicationRunListeners對象
		SpringApplicationRunListeners listeners = getRunListeners(args);
//執行SpringApplicationRunListener的starting();
		listeners.starting();
		try {
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
			ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
			configureIgnoreBeanInfo(environment);
//輸出Banner信息
			Banner printedBanner = printBanner(environment);
//經過SpringFactories構建ConfigurableApplicationContext對象
			context = createApplicationContext();
			exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
					new Class[] { ConfigurableApplicationContext.class }, context);
			prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//啓動web容器調用AnnotationConfigServletWebServerApplicationContext的refresh方法啓動web容器
			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;
	}

主要的技術點:java

SpringApplicationRunListener監聽器web

SpringFactoriesapp

AnnotationConfigServletWebServerApplicationContext webserver應用程序上下文處理類less

相關文章
相關標籤/搜索