springboot启动配置加载顺序问题

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要针对配置文件及配置中心(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的
在这里插入图片描述
从而完成了配置中心的配置,至此所有配置均已完成。

                                                                           -------------------------------->抛砖引玉
### Spring Boot 应用程序启动时文件的加载顺序 Spring Boot 应用程序在启动过程中会按照特定的顺序来解析配置属性和资源文件。以下是详细的加载流程: #### 配置文件解析优先级 当应用程序启动时,Spring Boot 会按如下顺序查找并加载配置文件中的属性[^1]: 1. **命令行参数** - 命令行传递给 JVM 的参数具有最高优先级。 2. **Java 系统属性 (`System.getProperties()`)** - 这些是由 `-D` 参数设置的 Java 属性。 3. **操作系统环境变量** - 来自操作系统的环境变量会被读取。 4. **随机数生成器 `random.*`** - 提供一些用于测试或开发目的的随机数值。 5. **JNDI 属性来自 java:comp/env** 6. **ServletConfig 初始化参数** 7. **ServletContext 初始化参数** 8. **位于类路径下的 `/config` 文件夹内的 profile-specific properties 文件** - 如 `application-prod.properties` 9. **位于当活动目录下 `/config` 文件夹内的 profile-specific properties 文件** 10. **位于类路径根部的 profile-specific properties 文件** 11. **位于当活动目录的 profile-specific properties 文件** 12. **位于类路径下的 `/config` 文件夹内的默认 properties 文件** - 即 `application.properties` 或者 `application.yml` 13. **位于当活动目录下 `/config` 文件夹内的默认 properties 文件** 14. **位于类路径根部的默认 properties 文件** 15. **位于当活动目录的默认 properties 文件** 16. **打包外部可执行 jar 中的 application.properties (仅限于带有嵌入式容器的应用)** 17. **通过 `@PropertySource` 注解显式指定的属性源** 18. **默认属性(由 SpringApplication.setDefaultProperties 指定)** 对于上述提到的各种方式定义的相同键值对,在面列出的方式将会覆盖后面列出的方式所定义的内容。 #### 特殊情况处理 如果指定了多个激活 profiles,则这些 profile 对应的配置文件也会被依次加载,并且较晚加载的文件可以覆盖之已有的同名属性值。此外,任何地方都可以使用占位符 `${...}` 形式的表达式引用其他已经存在的属性值[^2]。 ```yaml server: port: ${PORT:8080} ``` 此 YAML 片段表示服务器端口将尝试从名为 PORT 的环境变量获取,默认情况下为 8080。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值