springboot启动配置加载顺序问题

本文详细解析SpringBoot启动过程中的配置加载顺序,包括系统环境信息读取、配置文件加载及配置中心集成流程,以Apollo为例阐述配置中心的引入机制。

本文主要针对配置文件及配置中心(apollo为例)

springboot启动之后,

第一步 把相关listener、initializers加载进来。

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
        this.sources = new LinkedHashSet();
        this.bannerMode = Mode.CONSOLE;
        this.logStartupInfo = true;
        this.addCommandLineProperties = true;
        this.headless = true;
        this.registerShutdownHook = true;
        this.additionalProfiles = new HashSet();
        this.resourceLoader = resourceLoader;
       Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
        this.webApplicationType = this.deduceWebApplicationType();
        <-----------------------↓↓↓----------------------->
        this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
        <-----------------------↑↑↑----------------------->
        this.mainApplicationClass = this.deduceMainApplicationClass();
    }

initializers

listeners

第二步获取系统环境信息并读取配置信息

public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        this.configureHeadlessProperty();
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting();

        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            <-----------------------↓↓↓----------------------->
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            <-----------------------↑↑↑----------------------->
            this.configureIgnoreBeanInfo(environment);
            Banner printedBanner = this.printBanner(environment);
            context = this.createApplicationContext();
            this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            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 var9) {
            this.handleRunFailure(context, listeners, exceptionReporters, var9);
            throw new IllegalStateException(var9);
        }

        listeners.running(context);
        return context;
    }

在这里插入图片描述
接下来会触发environmentPrepared事件,在一些列调用后会发布ApplicationEnvironmentPreparedEvent事件,如下

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments) {
        ConfigurableEnvironment environment = this.getOrCreateEnvironment();
        this.configureEnvironment((ConfigurableEnvironment)environment, applicationArguments.getSourceArgs());
        <-----------------------↓↓↓----------------------->
        listeners.environmentPrepared((ConfigurableEnvironment)environment);
        <-----------------------↑↑↑----------------------->
        this.bindToSpringApplication((ConfigurableEnvironment)environment);
        if (this.webApplicationType == WebApplicationType.NONE) {
            environment = (new EnvironmentConverter(this.getClassLoader())).convertToStandardEnvironmentIfNecessary((ConfigurableEnvironment)environment);
        }
        ConfigurationPropertySources.attach((Environment)environment);
        return (ConfigurableEnvironment)environment;
    }

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里面我们主要关注这个类ConfigFileApplicationListener在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
接下来我们看这个方法

private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
1.        List<EnvironmentPostProcessor> postProcessors = this.loadPostProcessors();
2.        postProcessors.add(this);
3.        AnnotationAwareOrderComparator.sort(postProcessors);
4.        Iterator var3 = postProcessors.iterator();

        while(var3.hasNext()) {
            EnvironmentPostProcessor postProcessor = (EnvironmentPostProcessor)var3.next();
            postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication());
        }

    }

第一行获取

@FunctionalInterface
public interface EnvironmentPostProcessor {
    void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application);
}

EnvironmentPostProcessor 的实现方法,从META-INF/spring.factories中获取。
第二行把当前类,也就是ConfigFileApplicationListener这个类,因为这个类也实现了EnvironmentPostProcessor 接口,所以这个类也被加入了进来。

接下来就开始执行EnvironmentPostProcessor

我们只需要关注ConfigFileApplicationListener类中的如下方法。
在这里插入图片描述
这一步会完成

1.profile配置的相关工作

public void load() {
            this.profiles = Collections.asLifoQueue(new LinkedList());
            this.processedProfiles = new LinkedList();
            this.activatedProfiles = false;
            this.loaded = new LinkedHashMap();
            <-----------------------↓↓↓----------------------->
            this.initializeProfiles();
			<-----------------------↑↑↑----------------------->
            while(!this.profiles.isEmpty()) {
                ConfigFileApplicationListener.Profile profile = (ConfigFileApplicationListener.Profile)this.profiles.poll();
                this.load(profile, this::getPositiveProfileFilter, this.addToLoaded(MutablePropertySources::addLast, false));
                this.processedProfiles.add(profile);
            }
			<-----------------------↓↓↓----------------------->
            this.load((ConfigFileApplicationListener.Profile)null, this::getNegativeProfileFilter, this.addToLoaded(MutablePropertySources::addFirst, true));
            <-----------------------↑↑↑----------------------->
            this.addLoadedPropertySources();
        }

2.配置文件的读取工作,并添加到环境变量中

public void load() {
            this.profiles = Collections.asLifoQueue(new LinkedList());
            this.processedProfiles = new LinkedList();
            this.activatedProfiles = false;
            this.loaded = new LinkedHashMap();
            this.initializeProfiles();
            while(!this.profiles.isEmpty()) {
                ConfigFileApplicationListener.Profile profile = (ConfigFileApplicationListener.Profile)this.profiles.poll();
                this.load(profile, this::getPositiveProfileFilter, this.addToLoaded(MutablePropertySources::addLast, false));
                this.processedProfiles.add(profile);
            }
			<-----------------------↓↓↓----------------------->
            this.load((ConfigFileApplicationListener.Profile)null, this::getNegativeProfileFilter, this.addToLoaded(MutablePropertySources::addFirst, true));
            this.addLoadedPropertySources();
            <-----------------------↑↑↑----------------------->
        }

扩展:
在遍历配置文件的时候会按照如下规则进行查找

private Set<String> getSearchLocations() {
            if (this.environment.containsProperty("spring.config.location")) {
                return this.getSearchLocations("spring.config.location");
            } else {
                Set<String> locations = this.getSearchLocations("spring.config.additional-location");
                locations.addAll(this.asResolvedSet(ConfigFileApplicationListener.this.searchLocations, "classpath:/,classpath:/config/,file:./,file:./config/"));
                return locations;
            }
        }

至此本地的环境变量,配置文件已经读取完毕了,不过配置还缺少一步就是当我们用到配置中心的时候还需要执行如下操作,这里我们以apollo为例进行简单的说明。这个时候前面提到的initializers就派上用场了。看代码

3.配置中心配置的引入

public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        this.configureHeadlessProperty();
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting();

        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
            Banner printedBanner = this.printBanner(environment);
            context = this.createApplicationContext();
            this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            <-----------------------↓↓↓----------------------->
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            <-----------------------↑↑↑----------------------->
            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 var9) {
            this.handleRunFailure(context, listeners, exceptionReporters, var9);
            throw new IllegalStateException(var9);
        }

        listeners.running(context);
        return context;
    }
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
        context.setEnvironment(environment);
        this.postProcessApplicationContext(context);
        <-----------------------↓↓↓----------------------->
        this.applyInitializers(context);
        <-----------------------↑↑↑----------------------->
        listeners.contextPrepared(context);
        if (this.logStartupInfo) {
            this.logStartupInfo(context.getParent() == null);
            this.logStartupProfileInfo(context);
        }

        context.getBeanFactory().registerSingleton("springApplicationArguments", applicationArguments);
        if (printedBanner != null) {
            context.getBeanFactory().registerSingleton("springBootBanner", printedBanner);
        }

        Set<Object> sources = this.getAllSources();
        Assert.notEmpty(sources, "Sources must not be empty");
        this.load(context, sources.toArray(new Object[0]));
        listeners.contextLoaded(context);
    }

在this.applyInitializers(context);这里会执行apollo的
在这里插入图片描述
从而完成了配置中心的配置,至此所有配置均已完成。

                                                                           -------------------------------->抛砖引玉
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值