关于springboot:81SpringBoot启动流程把spring应用跑起来

6次阅读

共计 2490 个字符,预计需要花费 7 分钟才能阅读完成。

把 spring 利用跑起来

开始进入 run 办法:

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {return new SpringApplication(primarySources).run(args);
    }

step into run()

public ConfigurableApplicationContext run(String... args) {
        // 进行监听器,监控整个利用的启动和进行的
        StopWatch stopWatch = new StopWatch();
        //start 办法里的源码能够看上面代码展现
        stopWatch.start();// 记录利用的启动工夫
        // 创立疏导上下文(xxxContext 都是疏导环境)DefaultBootstrapContext bootstrapContext = createBootstrapContext();//createBootstrapContext 办法的源码在上面
        ConfigurableApplicationContext context = null;
        // 让以后利用进入 headless 模式。源码在上面
        //(headless 是自力更生的意思,不依赖他人)configureHeadlessProperty();
// 获取所有 RunListener(运行监听器)保留到 SpringApplicationRunListeners
//【为了不便所有 Listener 进行事件感知】所谓的监听器就是监听到以后我的项目的状态
        SpringApplicationRunListeners listeners = getRunListeners(args);
        //starting()源码剖析在上面
        listeners.starting(bootstrapContext, this.mainApplicationClass);
        try {
            // 筹备 ApplicationArguments 利用参数,利用参数是从命令行里传过来的。即保留命令行参数
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            // 筹备运行时环境
            ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);// 这行代码完结后,所有的环境信息曾经准备就绪
            // 配置一些要疏忽的 bean 信息
            configureIgnoreBeanInfo(environment);
            // 打印 banner(banner 是啥,能够看上面)Banner printedBanner = printBanner(environment);
            // 创立 ioc 容器(容器创立实现后,所有的 bean 都会进去)context = createApplicationContext();// 源码在上面
            // 把 Startup 信息保存起来
            context.setApplicationStartup(this.applicationStartup);
            // 筹备 ioc 容器环境的信息(源码在上面)prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
            // 刷新 ioc 容器 (办法的源码在上面)
            refreshContext(context);
//=============== 到这里为止,ioc 容器就创立实现了 ==================
            
            // 容器刷新实现后要做的工作
            afterRefresh(context, applicationArguments);
            // 监听到容器全副启动实现
            stopWatch.stop();
            if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
            }
            // 遍历所有监听器 调用 listeners.started(context); 
// 告诉所有的监听器 started 我的项目曾经启动了(告诉监听器又一个事干好了)所以在这里就能够拜访 8080 了
            listeners.started(context);
            // 调用所有 runners
            callRunners(context, applicationArguments);
        }
        catch (Throwable ex) {
            // 如果有异样,还会捕捉异样(捕捉源码在上面)handleRunFailure(context, ex, listeners);
            throw new IllegalStateException(ex);
        }

        try {// 没有任何异样就调用所有监听器的 running 办法  listeners.running(context); 
    // 告诉所有的监听器 running 我的项目曾经进入 running 状态了
            listeners.running(context);
        }
        catch (Throwable ex) {
            // 如果有异样,持续捕捉异样
            handleRunFailure(context, ex, null);
            throw new IllegalStateException(ex);
        }
        return context;
    }

banner:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
(()\___ | '_ |'_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.0)

正文完
 0