diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/bootstrap/ConfigServerBootstrapApplicationListener.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/bootstrap/ConfigServerBootstrapApplicationListener.java index 06e9ad5b..e0d0c4ef 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/bootstrap/ConfigServerBootstrapApplicationListener.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/bootstrap/ConfigServerBootstrapApplicationListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,10 @@ package org.springframework.cloud.config.server.bootstrap; import java.util.Collections; +import org.springframework.boot.SpringApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; +import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.context.ApplicationListener; import org.springframework.core.Ordered; import org.springframework.core.env.ConfigurableEnvironment; @@ -43,10 +45,15 @@ import org.springframework.core.env.PropertySource; * This listener is only to prevent it from using HTTP to contact itself. * * @author Dave Syer + * @author Ryan Baxter * */ +// TODO We changed this class from an ApplicationListener to an EnvironmentPostProcessor +// but did not change the class name to avoid breaking anyone in a minor release. +// In the next major it will be safe to change the class name and remove the +// ApplicationListener interface. public class ConfigServerBootstrapApplicationListener - implements ApplicationListener, Ordered { + implements ApplicationListener, EnvironmentPostProcessor, Ordered { /** * Default order of the bootstrap application listener. @@ -58,6 +65,15 @@ public class ConfigServerBootstrapApplicationListener private PropertySource propertySource = new MapPropertySource("configServerClient", Collections.singletonMap("spring.cloud.config.enabled", "false")); + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + if (!environment.resolvePlaceholders("${spring.cloud.config.enabled:false}").equalsIgnoreCase("true")) { + if (!environment.getPropertySources().contains(this.propertySource.getName())) { + environment.getPropertySources().addLast(this.propertySource); + } + } + } + @Override public int getOrder() { return this.order; @@ -69,12 +85,7 @@ public class ConfigServerBootstrapApplicationListener @Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { - ConfigurableEnvironment environment = event.getEnvironment(); - if (!environment.resolvePlaceholders("${spring.cloud.config.enabled:false}").equalsIgnoreCase("true")) { - if (!environment.getPropertySources().contains(this.propertySource.getName())) { - environment.getPropertySources().addLast(this.propertySource); - } - } + postProcessEnvironment(event.getEnvironment(), event.getSpringApplication()); } } diff --git a/spring-cloud-config-server/src/main/resources/META-INF/spring.factories b/spring-cloud-config-server/src/main/resources/META-INF/spring.factories index 906a6cf4..31eb4d95 100644 --- a/spring-cloud-config-server/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-config-server/src/main/resources/META-INF/spring.factories @@ -2,9 +2,11 @@ org.springframework.cloud.bootstrap.BootstrapConfiguration=\ org.springframework.cloud.config.server.bootstrap.ConfigServerBootstrapConfiguration,\ org.springframework.cloud.config.server.config.EncryptionAutoConfiguration -# Application listeners -org.springframework.context.ApplicationListener=\ + +# Environment PostProcessor +org.springframework.boot.env.EnvironmentPostProcessor=\ org.springframework.cloud.config.server.bootstrap.ConfigServerBootstrapApplicationListener + # Autoconfiguration org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.cloud.config.server.config.ConfigServerAutoConfiguration,\