spring的启动流程

1.创建监控器(监控运行时间)StopWatch stopWatch = new StopWatch();
2.创建空的IOC容器,和一组异常报告器
3.配置awt,图标配置
4.创建运行侦听器(SpringApplicationRunListeners listeners = this.getRunListeners(args);)
5.准备运行环境(DefaultApplicationArguments,默认应用程序参数)
6.配置系统参数(this.configureIgnoreBeanInfo(environment),environment,默认环境)
7.打印图标Banner(this.printBanner(environment);)
8.创建IOC容器映射(ApplicationContext)
9.初始化IOC容器(this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);)
10.刷新IOC容器(this.refreshContext(context);)
11.刷新最后的处理(this.afterRefresh(context, applicationArguments);) ,停止监听器(stopWatch.stop();)
12.发布运行侦听器 (this.callRunners(context, applicationArguments);)
13.运行器回调(打印信息,提示用户spring已经启动成功),spring启动完成

    public ConfigurableApplicationContext run(String... args) {
        //1.创建时间性能监控器
        StopWatch stopWatch = new StopWatch();
        //启动监听器
        stopWatch.start();
        //2.创建内容为null的IOC容器
        ConfigurableApplicationContext context = null;
        //创建异常报告
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        //3.配置awt(图标)的相关信息
        this.configureHeadlessProperty();
        //3.创建运行侦听器
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        //启动侦听器
        listeners.starting();

        Collection exceptionReporters;
        try {
            //5.准备运行时环境
            ApplicationArguments applicationArguments = 
                new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = 
                this.prepareEnvironment(listeners, applicationArguments);
            //6.配置系统参数
            this.configureIgnoreBeanInfo(environment);
            //7.打印图标
            Banner printedBanner = this.printBanner(environment);
            //8.创建IOC容器
            context = this.createApplicationContext();
            //设置异常报告
            exceptionReporters = this.getSpringFactoriesInstances(
                SpringBootExceptionReporter.class, new Class[]{
                    ConfigurableApplicationContext.class}, context);
            //9.初始化IOC容器
            this.prepareContext(context, environment, listeners,
                 applicationArguments, printedBanner);
            //10.刷新IOC容器
            this.refreshContext(context);
            //11.刷新后的处理
            this.afterRefresh(context, applicationArguments);
            //监听器停止
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass))
                    .logStarted(this.getApplicationLog(), stopWatch);
            }
            //12.发布started事件
            listeners.started(context);
            //13.运行器回调
            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);
        }
    }

    //3.配置awt的相关信息
    private void configureHeadlessProperty() {
        System.setProperty("java.awt.headless", System.getProperty(
            "java.awt.headless",     Boolean.toString(this.headless)));
    }
    
    //4.配置运行侦听器
    private SpringApplicationRunListeners getRunListeners(String[] args) {
        Class<?>[] types = new Class[]{SpringApplication.class, String[].class};
        return new SpringApplicationRunListeners(logger, 
                this.getSpringFactoriesInstances(SpringApplicationRunListener.class,
                     types, this, args));
    }

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sayhitoloverOvO

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

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

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

打赏作者

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

抵扣说明:

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

余额充值