zoukankan      html  css  js  c++  java
  • Spring Boot启动流程代码断点分析

     

    1. 启动入口

    1. 跟进run方法 : 一个用来使用默认的配置从特定的源运行SpringApplication的静态帮助类。

    这个类有两个重载方法,另一个用来传入多个源。通常,单个参数方法是数组方法的一个特例

    1. 创建一个新的SpringApplication实例。这个应用程序上下文会从特定的源加载Beans,这个实例会在调用run方法之前被定制化。

    Web应用程序类型的枚举:WebApplicationType,包含NONE(不是web应用),SERVLET(基于Servlet的web应用),REACTIVE(基于Reactive的web应用)

    • 直接jar包运行不使用web容器
    • 使用嵌入式的Servlet web容器
    • 使用反应式的web容器

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

    用于创建和加载Spring工厂方法实例

     4.运行SpringApplication的run方法

    Java SPI在 Spring Boot中的应用

    SpringBoot底层的自动化都是由这些SPI实现类来实现的:初始化,监听器,自动配置导入监听器,自动配置导入过滤器,自动配置,失败分析器,可用模板提供者

    Spring Boot找到main方式的方式

    通过抛异常的形式来获取堆栈信息,再获取启动类的信息。

    以上都是new SpringBootApplication的过程,下面分析run方法

    run方法分析

    /**
    * Run the Spring application, creating and refreshing a new
    * {@link ApplicationContext}.
    * 运行一个Spring应用,创建和刷新一个新的ApplicationContext
    * @param args the application arguments (usually passed from a Java main method)
    * 应用参数通过java main方法传递过来
    * @return a running {@link ApplicationContext}
    */
    public ConfigurableApplicationContext run(String... args) {
    // 任务计时器工具,可同时计数多个任务
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    //ApplicationContext是Spring的中心接口,为应用提供配置:1bean工厂2加载资源3注册的监听器发布事件4解析消息
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    //headless 模式:服务器端模式,表示系统没有键盘鼠标等前端应用
    configureHeadlessProperty();
    //监听器容器,对run方法各个阶段事件进行监听,观察者模式
    SpringApplicationRunListeners listeners = getRunListeners(args);
    //监听相应的事件,SpringApplicationEvent下的一个实现
    listeners.starting();
    try {
    //提供了对于运行SpringApplication参数的访问
    ApplicationArguments applicationArguments = new DefaultApplicationArguments(
    args);
    //环境配置:是servlet,reactive或者java应用环境,触发evn准备好的事件
    ConfigurableEnvironment environment = prepareEnvironment(listeners,
    applicationArguments);
    configureIgnoreBeanInfo(environment);
    Banner printedBanner = printBanner(environment);
    context = createApplicationContext();
    exceptionReporters = getSpringFactoriesInstances(
    SpringBootExceptionReporter.class,
    new Class[] { ConfigurableApplicationContext.class }, context);
    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;
    }
  • 相关阅读:
    mysql修改数据表名
    HDU 5742 It's All In The Mind (贪心)
    HDU 5752 Sqrt Bo (数论)
    HDU 5753 Permutation Bo (推导 or 打表找规律)
    HDU 5762 Teacher Bo (暴力)
    HDU 5754 Life Winner Bo (博弈)
    CodeForces 455C Civilization (并查集+树的直径)
    CodeForces 455B A Lot of Games (博弈论)
    CodeForces 455A Boredom (DP)
    HDU 4861 Couple doubi (数论 or 打表找规律)
  • 原文地址:https://www.cnblogs.com/fubinhnust/p/11930837.html
Copyright © 2011-2022 走看看