Spring Boot 启动流程

本文详细解读了Spring Boot应用从main方法启动到创建ApplicationContext的过程,重点介绍了webApplicationType的判断与Spring.factories文件的初始化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、spring boot 启动入口main方法

2、进入run()

进入run()方法后看到new SpringApplication对象,点击进入SpringApplication的构造方法中

3、进入SpringApplication构造方法

  1.  容器初始化

    this.webApplicationType = WebApplicationType.deduceFromClasspath();
    进入该方法该静态体中通过ClassUtils.isPresent()方法通过fromName来判断常量中的类是否加载过,并根据加载的类来判断返回的容器类型。
    webApplicationType对应的容器类型有三种如下:
    NONE :应用程序不作为web程序启动,不启动任何内嵌服务
    SERVLET:应用程序以基于servlet的web应用启动,需要启动内嵌的servlet web服务
    REACTIVE:应用程序以响应式web应用启动,需要启动内嵌的响应式的web服务
  2. 初始化spring.factories类

    setInitializers((Collection)getSpringFactoriesInstances(ApplicationContextInitializer.class));
    setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));

扫描当前目录下的spring.factories文件并分别加载 ApplicationContextInitializer和ApplicationListener实现类

4.进入run()方法的实现

public ConfigurableApplicationContext run(String... args) {
        // 1、设置计时器
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
        //2、配置环境变量awt
		configureHeadlessProperty();
        // 3、获取SpringApplicationRunListeners 监听器并执行
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.starting();
		try {
         // 4、将args封装成ApplicationArguments 对象
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
          // 5、准备上下文环境
			ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
           // 6、过滤掉不需要的bean
			configureIgnoreBeanInfo(environment);
            // 7、打印环境信息
			Banner printedBanner = printBanner(environment);
            // 8、创建上下文 判断webApplicationType
			context = createApplicationContext();
            // 9、获取异常监听
			exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
					new Class[] { ConfigurableApplicationContext.class }, context);
            // 10、准备上线文
			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;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值