SpringBoot自学的一些感悟

SpringBoot启动篇

SpringBoot前期的准备工作

以前有过很无知的想法,为什么SpringBoot或者Tomcat启动后不会像其他main方法一样执行完毕就没了,而前两者能一直等着数据请求,其实SpringBoot也只是利用了内嵌Tomcat容器,而Tomcat容器设置了守护线程而已。
以下是整体概览

public ConfigurableApplicationContext run(String... args) {
       //也只是执行了一些方法而已,传入的id为空
       /*    public StopWatch(String id) {
        this.keepTaskList = true;
        this.taskList = new LinkedList();
        this.id = id;
    }  */      
        StopWatch stopWatch = new StopWatch();
        //观察当前任务名是否不为空,不为空则说明已经启动,抛出如下错误
        //Can't start StopWatch: it's already running
        stopWatch.start();
        //预先创建IOC容器
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        //设置无论有没有显示器都可以启动
        this.configureHeadlessProperty();
        

// 探讨一:从spring.factories获取实现SpringApplicationRunListeners的类
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        //广播启动程序启动通知
        listeners.starting();

        Collection exceptionReporters;
        try {
        //封装args参数
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            //探讨二:准备环境变量,封装listener监听器,以及上述常见的实例
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            //忽略一些bean信息
            this.configureIgnoreBeanInfo(environment);
            //打印自定义Banner
            Banner printedBanner = this.printBanner(environment);
            //创建IOC容器
            context = this.createApplicationContext();
            //从spring.factories获取异常报告类,并封装到IOC容器context中
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            //对ioc容器进行一些处理,将环境变量、监听器、应用参数、打印banner都封装到ioc中
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            //这里是Spring的bean生命周期流程,暂时省略
            //this.refreshContext(context);
            //刷新容器之后对上下文进行一些处理
            this.afterRefresh(context, applicationArguments);
            
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }

            listeners.started(context);
            this.callRunners(context, applicationArguments);
        } catch (Throwable var10) {
            this.handleRunFailure(context, var10, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }

        try {
            listeners.running(context);
            return context;
        } catch (Throwable var9) {
            this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var9);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天边一头猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值