[DAY 7] Spring Boot 启动原理

在昨天了解如何建立专案後,今天来说明SPring Boot的启动原理

在不导入第三方library的情况下建立专案,专案除了Spring 的核心library以外,会自动产生[专案名]Application.java,在这里我们以建立专案名demo为後面容易称呼底下元件,而底下会建立DemoApplication.java。
建立出来的专案如下

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

首先先看main方法里面的SpringApplication.run(Class<?> primarySource, String... args)方法

	/**
	 * Static helper that can be used to run a {@link SpringApplication} from the
	 * specified source using default settings.
	 * @param primarySource the primary source to load
	 * @param args the application arguments (usually passed from a Java main method)
	 * @return the running {@link ApplicationContext}
	 */
	public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
		return run(new Class<?>[] { primarySource }, args);
	}

这方法回传了ConfigurableApplicationContext元件,实际去呼叫run(Class<?>[] primarySources, String[] args)方法,继续向下查询看到以下结果

	/**
	 * Static helper that can be used to run a {@link SpringApplication} from the
	 * specified sources using default settings and user supplied arguments.
	 * @param primarySources the primary sources to load
	 * @param args the application arguments (usually passed from a Java main method)
	 * @return the running {@link ApplicationContext}
	 */
	public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
		return new SpringApplication(primarySources).run(args);
	}

这里可以看到,呼叫了SpringApplication(Class<?>... primarySources)的constructor,并且执行了run(String... args) 方法,先来看constructor

/**
	 * 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 primarySources the primary bean sources
	 * @see #run(Class, String[])
	 * @see #SpringApplication(ResourceLoader, Class...)
	 * @see #setSources(Set)
	 */
	public SpringApplication(Class<?>... primarySources) {
		this(null, primarySources);
	}

实际是呼叫了另一个constructor SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources)

/**
	 * 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();
		this.bootstrapRegistryInitializers = getBootstrapRegistryInitializersFromSpringFactories();
		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		this.mainApplicationClass = deduceMainApplicationClass();
	}

在这里实现了SpringBoot核心的init设定

再来看run(String... args)方法

/**
	 * Run the Spring application, creating and refreshing a new
	 * {@link ApplicationContext}.
	 * @param args the application arguments (usually passed from a Java main method)
	 * @return a running {@link ApplicationContext}
	 */
	public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		DefaultBootstrapContext bootstrapContext = createBootstrapContext();
		ConfigurableApplicationContext context = null;
		configureHeadlessProperty();
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.starting(bootstrapContext, this.mainApplicationClass);
		try {
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
			ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
			configureIgnoreBeanInfo(environment);
			Banner printedBanner = printBanner(environment);
			context = createApplicationContext();
			context.setApplicationStartup(this.applicationStartup);
			prepareContext(bootstrapContext, 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, listeners);
			throw new IllegalStateException(ex);
		}

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

这里利用SpringApplicationRunListeners.starting实现了监听Configuration设定,另外是环境的容器化将properties设定引入,然後实际去ConfigurableApplicationContext的启用,并监听它

在简单说明的DemoApplication.java的启动方法後,明天来说明annotation的运作与功能


<<:  Day11 - Button(一)

>>:  Kotlin Android 第17天,从 0 到 ML - MVVM架构 - LiveData

Day.20 Course Schedule

Leetcode #207. Course Schedule 题目给你一系列的课程,每一个门课都有它...

Day 27 | SQLite资料库(二)

资料库结构与设计 //建立资料表 CREATE TABLE myTable() 表格名称後的括号内,...

.NET 新手 无痛入职 _ Day3 建置专案(VS2019)

步骤1. 首先我们先到Microsoft官网下载Visual Studio 2019 的社群版本(C...

Python - Django 参考笔记

Python - Django 参考笔记 参考资料 参考资料: 架设 Django 开发环境 Day...

Core Web Vitals 的新指标 INP

在今年的 Google I/O 大会中,Google 介绍了一个名为 INP(Interaction...