美文网首页
SpringBoot-application.yml的加载

SpringBoot-application.yml的加载

作者: AlanSun2 | 来源:发表于2019-09-27 15:07 被阅读0次

SpringBoot 版本:2.1.7.RELEASE

你可知道SpringBoot中的 application.properties 或 application.yml如果被加载吗?

答案就在ConfigFileApplicationListener里。

下面我们就来解释下它的加载过程:
SpringApplication#run()

public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
        configureHeadlessProperty();
        SpringApplicationRunListeners listeners = getRunListeners(args);
        listeners.starting();
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
这里准备环境
            ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
            configureIgnoreBeanInfo(environment);
            Banner printedBanner = printBanner(environment);
            context = createApplicationContext();
            exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                    new Class[] { ConfigurableApplicationContext.class }, context);
            prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            refreshContext(context);
            afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
            }
            listeners.started(context);
            callRunners(context, applicationArguments);
        }
        catch (Throwable ex) {
            handleRunFailure(context, ex, exceptionReporters, listeners);
            throw new IllegalStateException(ex);
        }

        try {
            listeners.running(context);
        }
        catch (Throwable ex) {
            handleRunFailure(context, ex, exceptionReporters, null);
            throw new IllegalStateException(ex);
        }
        return context;
    }
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
            ApplicationArguments applicationArguments) {
        // Create and configure the environment
        ConfigurableEnvironment environment = getOrCreateEnvironment();
        configureEnvironment(environment, applicationArguments.getSourceArgs());
//发布已准备好环境的通知,发布ApplicationEnvironmentPreparedEvent
        listeners.environmentPrepared(environment);
        bindToSpringApplication(environment);
        if (!this.isCustomEnvironment) {
            environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
                    deduceEnvironmentClass());
        }
        ConfigurationPropertySources.attach(environment);
        return environment;
    }

因为ConfigFileApplicationListener实现了SmartApplicationListener,SmartApplicationListener会对所有的时间进行监听:

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationEnvironmentPreparedEvent) {
            onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
        }
        if (event instanceof ApplicationPreparedEvent) {
            onApplicationPreparedEvent(event);
        }
    }

    private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
        List<EnvironmentPostProcessor> postProcessors = loadPostProcessors();
        postProcessors.add(this);
        AnnotationAwareOrderComparator.sort(postProcessors);
        for (EnvironmentPostProcessor postProcessor : postProcessors) {
            postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication());
        }
    }

这里初始化EnvironmentPostProcessor(即环境准备好后,对环境进行处理),postProcessors有四个EnvironmentPostProcessor

这篇文章还不错
https://www.cnblogs.com/youzhibing/p/9622441.html

相关文章

  • SpringBoot-application.yml的加载

    SpringBoot 版本:2.1.7.RELEASE 你可知道SpringBoot中的 application....

  • 【原创】unity简单封装实现一个进度加载 和文本提示

    首先,加载中 显示加载动画和正在加载, 加载完成 隐藏,加载失败 则显示重新加载按钮和 加载失败的文字。 要解决自...

  • 第一章 类加载过程

    要点 类加载过程 类加载器 一、类加载过程 1.类的加载过程 类的加载 .class文件过程分为:加载---->连...

  • android随笔之图片加载Glide、Fresco、Picas

    Glide: 功能特点: 1,图片异步加载,设置加载尺寸,设置加载动画,设置加载中和加载失败的图片,加载的图片格式...

  • 2018-07-17 懒加载和预加载

    懒加载也就是延迟加载。懒加载的核心的思想是延迟加载,需使用到的时候才进行加载,不使用的时候是不会加载的 预加载提前...

  • 类的加载过程(含面试题)

    类的加载过程 loading 加载通过双亲委派机制进行加载。主要出于安全的考虑。父加载器不是加载器的加载器,也不是...

  • 深入理解jvm类加载机制

    1.什么是类加载? 类加载机制一个很大的体系,包括类加载的时机,类加载器,类加载时机。 1.1类加载过程 加载器加...

  • Bug-遇见-iOS

    加载xib 崩溃 1.正确的加载xib的方式加载ViewController 加载UIView

  • 图片懒加载

    懒加载与预加载的基本概念。 懒加载也叫延迟加载:JS图片延迟加载 延迟加载图片或符合某些条件时才加载某些图片。 预...

  • 01-类加载过程

    类加载过程 类加载可以分为加载、连接、初始化3个部分 加载 加载过程是指查找并加载类的二进制数据,加载class文...

网友评论

      本文标题:SpringBoot-application.yml的加载

      本文链接:https://www.haomeiwen.com/subject/jnvdyctx.html