简单读下SpringBoot run方法执行过程

    public ConfigurableApplicationContext run(String... args) {
        // 执行时间跟踪
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
        // 标记无图形界面启动
        configureHeadlessProperty();
        // 获取SpringBoot的监听器,找的是spring.factories里面实现了SpringApplicationRunListener接口的
        SpringApplicationRunListeners listeners = getRunListeners(args);
        // 发布容器开始启动事件
        listeners.starting();
        try {
            // 包装下main方法传进来的那个args数组
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            // 构建系统环境变量
            ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
            // 决定是否跳过 BeanInfo 类的扫描,spring.beaninfo.ignore 的配置用来,如果设置为 true,则跳过
            configureIgnoreBeanInfo(environment);
            // 打印那个Banner图
            Banner printedBanner = printBanner(environment);
            // 根据webApplicationType反射创建ApplicationContext
            context = createApplicationContext();
            // 收集异常报告实例集合,用于catch里面处理
            exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                    new Class[] { ConfigurableApplicationContext.class }, context);
            // 预准备容器
            // context.setEnvironment(environment),把环境变量set进容器
            // postProcessApplicationContext(context);前置准备,主要是一些类加载器
            // applyInitializers(context),执行ApplicationContextInitializer的初始化方法
            // listeners.contextPrepared(context);发布容器预准备事件
            // 往BeanFactory里的map直接丢一些bean,比如environment applicationArguments
            prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            // 刷新容器,执行AbstractApplicationContext里面的refresh方法,执行spring的容器初始化
            refreshContext(context);
            // 空的扩展方法
            afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
            }
            // 发布容器已启动
            listeners.started(context);
            // 执行ApplicationRunner,CommandLineRunner的实现类
            callRunners(context, applicationArguments);
        }
        catch (Throwable ex) {
            // 异常处理
            handleRunFailure(context, ex, exceptionReporters, listeners);
            throw new IllegalStateException(ex);
        }

        try {
            // 发布容器运行中事件
            listeners.running(context);
        }
        catch (Throwable ex) {
            // 异常处理,执行SpringBootExceptionReporter集
            handleRunFailure(context, ex, exceptionReporters, null);
            throw new IllegalStateException(ex);
        }
        return context;
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值