From 3885af8caf641b805701f23fcc9342a325ef4a9d Mon Sep 17 00:00:00 2001 From: spencergibb Date: Mon, 15 Mar 2021 20:27:14 -0400 Subject: [PATCH] Moves bootstrap enabled property to refresh property source. LegacyContextRefresher used to set `spring.cloud.bootstrap.enabled=true` as a property using the api of `SpringApplicationBuilder`. This change moves the setting of properties for refresh from ContextRefresher to LegacyContextRefresher since they are only applicable there. The bootstrap enabled propery is now set on the refresh property source rather than the api. Fixes gh-892 --- .../bootstrap/encrypt/TextEncryptorUtils.java | 7 ++++--- .../cloud/context/refresh/ContextRefresher.java | 7 ------- .../context/refresh/LegacyContextRefresher.java | 15 +++++++++++++-- .../context/refresh/ContextRefresherTests.java | 1 - 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/TextEncryptorUtils.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/TextEncryptorUtils.java index 2c1b7354..e59068af 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/TextEncryptorUtils.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/TextEncryptorUtils.java @@ -70,7 +70,8 @@ public abstract class TextEncryptorUtils { } /** - * Register all classes that need a {@link TextEncryptor} in {@link TextEncryptorConfigBootstrapper}. + * Register all classes that need a {@link TextEncryptor} in + * {@link TextEncryptorConfigBootstrapper}. * @param registry the BootstrapRegistry. */ public static void register(BootstrapRegistry registry) { @@ -156,8 +157,8 @@ public abstract class TextEncryptorUtils { } /** - * Method to check if legacy bootstrap mode is enabled. This is either if the - * boot legacy processing property is set or spring.cloud.bootstrap.enabled=true. + * Method to check if legacy bootstrap mode is enabled. This is either if the boot + * legacy processing property is set or spring.cloud.bootstrap.enabled=true. * @param environment where to check properties. * @return true if bootstrap enabled. */ diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java index 36156566..e3f575d2 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java @@ -36,7 +36,6 @@ import org.springframework.core.env.CommandLinePropertySource; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.EnumerablePropertySource; -import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; @@ -131,12 +130,6 @@ public abstract class ContextRefresher { } environment.setActiveProfiles(input.getActiveProfiles()); environment.setDefaultProfiles(input.getDefaultProfiles()); - Map map = new HashMap(); - map.put("spring.jmx.enabled", false); - map.put("spring.main.sources", ""); - // gh-678 without this apps with this property set to REACTIVE or SERVLET fail - map.put("spring.main.web-application-type", "NONE"); - capturedPropertySources.addFirst(new MapPropertySource(REFRESH_ARGS_PROPERTY_SOURCE, map)); return environment; } diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/LegacyContextRefresher.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/LegacyContextRefresher.java index 534ff0e2..7f20daf2 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/LegacyContextRefresher.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/LegacyContextRefresher.java @@ -17,6 +17,8 @@ package org.springframework.cloud.context.refresh; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; import org.springframework.boot.Banner; import org.springframework.boot.WebApplicationType; @@ -26,6 +28,7 @@ import org.springframework.cloud.bootstrap.BootstrapApplicationListener; import org.springframework.cloud.bootstrap.BootstrapConfigFileApplicationListener; import org.springframework.cloud.context.scope.refresh.RefreshScope; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; @@ -57,8 +60,16 @@ public class LegacyContextRefresher extends ContextRefresher { ConfigurableApplicationContext capture = null; try { StandardEnvironment environment = copyEnvironment(getContext().getEnvironment()); - SpringApplicationBuilder builder = new SpringApplicationBuilder(Empty.class) - .properties(BOOTSTRAP_ENABLED_PROPERTY + "=true").bannerMode(Banner.Mode.OFF) + + Map map = new HashMap<>(); + map.put("spring.jmx.enabled", false); + map.put("spring.main.sources", ""); + // gh-678 without this apps with this property set to REACTIVE or SERVLET fail + map.put("spring.main.web-application-type", "NONE"); + map.put(BOOTSTRAP_ENABLED_PROPERTY, Boolean.TRUE.toString()); + environment.getPropertySources().addFirst(new MapPropertySource(REFRESH_ARGS_PROPERTY_SOURCE, map)); + + SpringApplicationBuilder builder = new SpringApplicationBuilder(Empty.class).bannerMode(Banner.Mode.OFF) .web(WebApplicationType.NONE).environment(environment); // Just the listeners that affect the environment (e.g. excluding logging // listener because it has side effects) diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java index c498d5da..76619d7b 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java @@ -78,7 +78,6 @@ public class ContextRefresherTests { } @Test - @Ignore // FIXME: legacy public void bootstrapPropertySourceAlwaysFirst() { // Use spring.cloud.bootstrap.name to switch off the defaults (which would pick up // a bootstrapProperties immediately