【SpringBoot】启动流程

SpringBoot版本–2.1.5
1、创建SpringApplication对象

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
        this.sources = new LinkedHashSet();
        this.bannerMode = Mode.CONSOLE;
        this.logStartupInfo = true;
        this.addCommandLineProperties = true;
        this.addConversionService = true;
        this.headless = true;
        this.registerShutdownHook = true;
        this.additionalProfiles = new HashSet();
        this.isCustomEnvironment = false;
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
        //判断应用是否为Web应用
        this.webApplicationType = WebApplicationType.deduceFromClasspath();
        //从类路径下的META-INF/spring.factories找到所有ApplicationContextInitializer并保存
		this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
		//从类路径下的META-INF/spring.factories找到所有ApplicationListener并保存
        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
        //从所有的配置类中找到带有main方法的主配置类
        this.mainApplicationClass = this.deduceMainApplicationClass();
    }

2、运行run方法

public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        this.configureHeadlessProperty();
        //从类路径下的MATE-INF/spring.factories中获取所有的SpringApplicationRunListener
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        //回调所有SpringApplicationRunListener的starting()方法
        listeners.starting();

        Collection exceptionReporters;
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            /*
             * 1、获取或创建环境
             * 2、回调SpringApplicationRunListener的environmentPrepared()方法,表示环境准备完成
            */
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
            Banner printedBanner = this.printBanner(environment);
            //利用BeanUtils.instantiateClass(contextClass)创建Spring容器
            context = this.createApplicationContext();
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            /*
             * 准备上下文环境
             * 1、回调所有SpringApplicationRunListener的initialize()方法
             * 2、回调所有SpringApplicationRunListener的contextPrepared()方法
             * 3、回调所有SpringApplicationRunListener的contextLoaded()方法
            */
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            //初始化IOC容器
            this.refreshContext(context);
            //2.1.5版本未提供默认实现,需要由子类实现
            this.afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }
			//回调所有SpringApplicationRunListener的started()方法
            listeners.started(context);
            //回调所有ApplicationRunner的run()方法
            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);
        }
    }

通过分析SpringBoot的启动过程得知,可以通过实现自己的SpringApplicationRunListener、ApplicationRunner对SpringBoot的启动过程进行干预,从而实现对SpringBoot的扩展。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值