Spring Boot源码

public ConfigurableApplicationContext run(String... args) {
    任务执行时长spring

    StopWatch stopWatch = new StopWatch(); app

                  stopWatch.start();less

     Spring 上下文:ConfigurableApplicationContext
              ConfigurableApplicationContext context = null;
     Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
     this

     Headless模式是在缺乏显示屏、键盘或者鼠标是的系统配置。
                  Headless模式是系统的一种配置模式。在系统可能缺乏显示设备、键盘或鼠标这些外设的状况下可使用该模式。
               spa

               configureHeadlessProperty();

     实例化监听spring的监听器,EventPublishingRunListener,根据配置文件,日志

     以及过滤器SpringApplicationRunListener,实例化监听对象,经过class.forName.code

               SpringApplicationRunListeners listeners = getRunListeners(args);
                启动事件监听对象

               listeners.starting();blog

              try {
         构造容器参数信息接口

                         ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);

准备环境变量,初始为StandardServletEnvironment
标准的servlet环境。给上边的监听对象,绑定环境变量信息 具体参考里边的方法。

                         ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);
       经过配置忽略Bean的信息

                          configureIgnoreBeanInfo(environment);
       打印 banner日志。

                        Banner printedBanner = printBanner(environment);
       建立springApplication上下文。根据servlet类型:实例的对象是:
                         AnnotationConfigServletWebServerApplicationContext

                        context = createApplicationContext();
                        从配置文件中加载,加载SpringBootExceptionReporter的子类。
                        exceptionReporters = getSpringFactoriesInstances(

                                             SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context);

                        准备上下文信息。

                       prepareContext(context, environment, listeners, applicationArguments,printedBanner);

                        刷新容器信息,采用的是spring的加载过程。

                        refreshContext(context);

                        afterRefresh(context, applicationArguments);

                        stopWatch.stop();

                        if (this.logStartupInfo) {

                              new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);

                        }

                       // 监听执行started方法

                        listeners.started(context);、

        // 执行CommandLineRunner 和ApplicationRunner的方法

                        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;  

}

SpringApplicationRunListener :接口说明

 

主要完成boot启动的时,执行的本身得一套逻辑代码,能够根据资源,容器的初始话过程,执行不一样的逻辑代码信息

 


public interface SpringApplicationRunListener {


void starting();

/**
* Called once the environment has been prepared, but before the
* {@link ApplicationContext} has been created.
* @param environment the environment
*/
void environmentPrepared(ConfigurableEnvironment environment);

/**
* Called once the {@link ApplicationContext} has been created and prepared, but
* before sources have been loaded.
* @param context the application context
*/
void contextPrepared(ConfigurableApplicationContext context);

/**
* Called once the application context has been loaded but before it has been
* refreshed.
* @param context the application context
*/
void contextLoaded(ConfigurableApplicationContext context);

/**
* The context has been refreshed and the application has started but
* {@link CommandLineRunner CommandLineRunners} and {@link ApplicationRunner
* ApplicationRunners} have not been called.
* @param context the application context.
* @since 2.0.0
*/
void started(ConfigurableApplicationContext context);

/**
* Called immediately before the run method finishes, when the application context has
* been refreshed and all {@link CommandLineRunner CommandLineRunners} and
* {@link ApplicationRunner ApplicationRunners} have been called.
* @param context the application context.
* @since 2.0.0
*/
void running(ConfigurableApplicationContext context);

/**
* Called when a failure occurs when running the application.
* @param context the application context or {@code null} if a failure occurred before
* the context was created
* @param exception the failure
* @since 2.0.0
*/
void failed(ConfigurableApplicationContext context, Throwable exception);

}

相关文章
相关标签/搜索