Spring Boot系列--启动方法prepareContext

本文深入探讨了Spring Boot应用程序的启动过程,详细分析了run方法中的prepareContext步骤,包括环境配置、异常报告器加载、上下文初始化等关键环节。

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

参考版本:2.0.8.RELEASE

启动方法run中prepareContext方法的执行

/**
 * 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();
   ConfigurableApplicationContext context = null;
   Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
   configureHeadlessProperty();
//启动Spring应用运行监听器
   SpringApplicationRunListeners listeners = getRunListeners(args);
   listeners.starting();
   try {
      ApplicationArguments applicationArguments = new DefaultApplicationArguments(
            args);
//配置环境
      ConfigurableEnvironment environment = prepareEnvironment(listeners,
            applicationArguments);
      configureIgnoreBeanInfo(environment);
//打印banner
      Banner printedBanner = printBanner(environment);
//创建ApplicationContext,并确定类型
      context = createApplicationContext();
//加载异常报告器类并实例化
      exceptionReporters = getSpringFactoriesInstances(
            SpringBootExceptionReporter.class,
            new Class[] { ConfigurableApplicationContext.class }, context);
//Spring上下文前置处理
      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;
}
private void prepareContext(ConfigurableApplicationContext context,
      ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
      ApplicationArguments applicationArguments, Banner printedBanner) {
   context.setEnvironment(environment);
   postProcessApplicationContext(context);
//应用上下文的初始化器,执行一些初始化操作
   applyInitializers(context);
//
   listeners.contextPrepared(context);
   if (this.logStartupInfo) {
//打印启动日志信息
      logStartupInfo(context.getParent() == null);
      logStartupProfileInfo(context);
   }

   // Add boot specific singleton beans注册两个特殊的单例
   context.getBeanFactory().registerSingleton("springApplicationArguments",
         applicationArguments);
   if (printedBanner != null) {
      context.getBeanFactory().registerSingleton("springBootBanner", printedBanner);
   }

   // Load the sources
   Set<Object> sources = getSources();
   Assert.notEmpty(sources, "Sources must not be empty");
   load(context, sources.toArray(new Object[sources.size()]));
   listeners.contextLoaded(context);
}

 关于初始化器的解释可参见https://blog.youkuaiyun.com/andy_zhang2007/article/details/84032061,回头再自己整理

 

 

Spring Boot的源码分析涉及到多个类和方法。其中,方法org.springframework.boot.SpringApplication#prepareContext用于准备应用程序的上下文,并返回一个配置好的Spring应用程序上下文。 Spring Boot的主要目的是简化Spring框架中繁复的配置问题。它通过提供自动配置、约定优于配置和快速开发的原则来实现这一目标。具体来说,它使用了一系列机制来简化开发者的工作,包括自动配置、自动扫描、自动装配等。 在Spring Boot应用程序中,这些机制通常会在应用程序启动时加载到IOC容器中。例如,在一个典型的Spring Boot应用程序的入口类中,使用@SpringBootApplication注解标记了应用程序的主类,并使用@MapperScan注解指定了需要扫描的Mapper接口所在的包。在main方法中,通过调用SpringApplication.run方法启动应用程序,并将主类和命令行参数传递给它。在运行过程中,Spring Boot会自动扫描应用程序中的注解和配置,并将相应的Bean加载到IOC容器中。 总的来说,Spring Boot的源码分析涉及到Spring Boot的核心类和方法,以及它提供的各种机制和配置方式。通过深入研究这些源码,可以更好地理解Spring Boot的工作原理和内部机制。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [springboot源码分析](https://blog.youkuaiyun.com/xiaoxiaoyeyaya/article/details/108560836)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot源码分析](https://blog.youkuaiyun.com/mutf7/article/details/119387639)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值