设为首页 加入收藏

TOP

SpringBoot启动流程(二)
2019-09-07 07:09:54 】 浏览:74
Tags:SpringBoot 启动 流程
nstances(ApplicationListener.class)); //找出main类,也就是SpringBoot项目的主类 this.mainApplicationClass = deduceMainApplicationClass(); }

2.1 run方法完整代码

执行完初始化之后回到run()方法中,完整代码如下:

/**
* 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;
  FailureAnalyzers analyzers = null;
  configureHeadlessProperty();
   //创建应用监听器
  SpringApplicationRunListeners listeners = getRunListeners(args);
  //开始监听
  listeners.starting();
  
  try {
     ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
     //加载SpringBoot配置环境ConfigurableEnvironment,见2.2配置ConfigurableEnvironment
     ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);
     //打印banner
     Banner printedBanner = printBanner(environment);
     //创建应用程序上下文,见2.3 创建应用程序上下文
     context = createApplicationContext();
     analyzers = new FailureAnalyzers(context);
     prepareContext(context, environment, listeners, applicationArguments,printedBanner);
     refreshContext(context);
     afterRefresh(context, applicationArguments);
     listeners.finished(context, null);
     stopWatch.stop();
     if (this.logStartupInfo) {
      new StartupInfoLogger(this.mainApplicationClass)
            .logStarted(getApplicationLog(), stopWatch);
     }
     return context;
   }catch (Throwable ex) {
     handleRunFailure(context, listeners, analyzers, ex);
     throw new IllegalStateException(ex);
   }
}

2.2 配置ConfigurableEnvironment

加载SpringBoot配置环境ConfigurableEnvironment流程如下:

private ConfigurableEnvironment prepareEnvironment(
            SpringApplicationRunListeners listeners,
            ApplicationArguments applicationArguments) {
        // Create and configure the environment
        ConfigurableEnvironment environment = getOrCreateEnvironment();
        configureEnvironment(environment, applicationArguments.getSourceArgs());
        listeners.environmentPrepared(environment);
        if (!this.webEnvironment) {
            environment = new EnvironmentConverter(getClassLoader())
                    .convertToStandardEnvironmentIfNecessary(environment);
        }
        return environment;
    }

在加载配置环境的过程中会判断是否是web容器启动,如果是容器启动会加载StandardServletEnvironment

private ConfigurableEnvironment getOrCreateEnvironment() {
        if (this.environment != null) {
            return this.environment;
        }
        if (this.webEnvironment) {
            return new StandardServletEnvironment();
        }
        return new StandardEnvironment();
    }

StandardServletEnvironment类的继承关系如下,StandardServletEnvironment

StandardServletEnvironment类关系图.png

PropertyResolver接口是用于解析任何基础源的属性的接口,在加载完配置之后会将配置环境加入到监听器对象SpringApplicationRunListeners中。

2.3 创建应用程序上下文

然后会创建应用上下文对象,具体代码如下:

protected ConfigurableApplicationContext createApplicationContext() {
        Class<?> contextClass = this.applicationContextClass;
        if (contextClass == null) {
            try {
                contextClass = Class.forName(this.webEnvironment
                        ? DEFAULT_WEB_CONTEXT_CLASS : DEFAULT_CONTEXT_CLASS);
            }
            catch (ClassNotFoundException ex) {
                throw new IllegalStateException(
                        "Unable create a default ApplicationContext, "
                                + "please specify an ApplicationContextClass",
                        ex);
            }
        }
        retur
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇springboot结合日志门面SLF4j和日.. 下一篇spring data jpa碰到的坑

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目