栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring的启动流程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 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));
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/337994.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号