Retain configured property sources across refresh.
Fixes gh-913
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package org.springframework.cloud.autoconfigure;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
@@ -36,6 +37,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.cloud.context.refresh.ConfigDataContextRefresher;
|
||||
@@ -53,6 +56,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.weaving.LoadTimeWeaverAware;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.instrument.classloading.LoadTimeWeaver;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -68,6 +72,7 @@ import org.springframework.util.StringUtils;
|
||||
@ConditionalOnClass(RefreshScope.class)
|
||||
@ConditionalOnProperty(name = RefreshAutoConfiguration.REFRESH_SCOPE_ENABLED, matchIfMissing = true)
|
||||
@AutoConfigureBefore(HibernateJpaAutoConfiguration.class)
|
||||
@EnableConfigurationProperties(RefreshAutoConfiguration.RefreshProperties.class)
|
||||
public class RefreshAutoConfiguration {
|
||||
|
||||
/**
|
||||
@@ -100,16 +105,17 @@ public class RefreshAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnBootstrapEnabled
|
||||
public LegacyContextRefresher legacyContextRefresher(ConfigurableApplicationContext context, RefreshScope scope) {
|
||||
return new LegacyContextRefresher(context, scope);
|
||||
public LegacyContextRefresher legacyContextRefresher(ConfigurableApplicationContext context, RefreshScope scope,
|
||||
RefreshProperties properties) {
|
||||
return new LegacyContextRefresher(context, scope, properties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnBootstrapDisabled
|
||||
public ConfigDataContextRefresher configDataContextRefresher(ConfigurableApplicationContext context,
|
||||
RefreshScope scope) {
|
||||
return new ConfigDataContextRefresher(context, scope);
|
||||
RefreshScope scope, RefreshProperties properties) {
|
||||
return new ConfigDataContextRefresher(context, scope, properties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -117,6 +123,33 @@ public class RefreshAutoConfiguration {
|
||||
return new RefreshEventListener(contextRefresher);
|
||||
}
|
||||
|
||||
@ConfigurationProperties("spring.cloud.refresh")
|
||||
public static class RefreshProperties {
|
||||
|
||||
/**
|
||||
* Additional property sources to retain during a refresh. Typically only system
|
||||
* property sources are retained. This property allows property sources, such as
|
||||
* property sources created by EnvironmentPostProcessors to be retained as well.
|
||||
*/
|
||||
private List<String> additionalPropertySourcesToRetain;
|
||||
|
||||
public List<String> getAdditionalPropertySourcesToRetain() {
|
||||
return this.additionalPropertySourcesToRetain;
|
||||
}
|
||||
|
||||
public void setAdditionalPropertySourcesToRetain(List<String> additionalPropertySourcesToRetain) {
|
||||
this.additionalPropertySourcesToRetain = additionalPropertySourcesToRetain;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringCreator(this)
|
||||
.append("additionalPropertySourcesToRetain", additionalPropertySourcesToRetain).toString();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(name = "javax.persistence.EntityManagerFactory")
|
||||
protected static class JpaInvokerConfiguration implements LoadTimeWeaverAware {
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.cloud.context.refresh;
|
||||
|
||||
import org.springframework.boot.DefaultBootstrapContext;
|
||||
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
|
||||
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
|
||||
import org.springframework.cloud.context.scope.refresh.RefreshScope;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
@@ -31,10 +32,16 @@ import org.springframework.core.io.DefaultResourceLoader;
|
||||
*/
|
||||
public class ConfigDataContextRefresher extends ContextRefresher {
|
||||
|
||||
@Deprecated
|
||||
public ConfigDataContextRefresher(ConfigurableApplicationContext context, RefreshScope scope) {
|
||||
super(context, scope);
|
||||
}
|
||||
|
||||
public ConfigDataContextRefresher(ConfigurableApplicationContext context, RefreshScope scope,
|
||||
RefreshAutoConfiguration.RefreshProperties properties) {
|
||||
super(context, scope, properties);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateEnvironment() {
|
||||
if (logger.isTraceEnabled()) {
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.util.Set;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
|
||||
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
|
||||
import org.springframework.cloud.context.scope.refresh.RefreshScope;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -39,6 +40,7 @@ import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.context.support.StandardServletEnvironment;
|
||||
|
||||
/**
|
||||
@@ -62,13 +64,23 @@ public abstract class ContextRefresher {
|
||||
StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME,
|
||||
StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, "configurationProperties"));
|
||||
|
||||
protected final List<String> additionalPropertySourcesToRetain;
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
private RefreshScope scope;
|
||||
|
||||
@Deprecated
|
||||
protected ContextRefresher(ConfigurableApplicationContext context, RefreshScope scope) {
|
||||
this(context, scope, new RefreshAutoConfiguration.RefreshProperties());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected ContextRefresher(ConfigurableApplicationContext context, RefreshScope scope,
|
||||
RefreshAutoConfiguration.RefreshProperties properties) {
|
||||
this.context = context;
|
||||
this.scope = scope;
|
||||
additionalPropertySourcesToRetain = properties.getAdditionalPropertySourcesToRetain();
|
||||
}
|
||||
|
||||
protected ConfigurableApplicationContext getContext() {
|
||||
@@ -102,7 +114,12 @@ public abstract class ContextRefresher {
|
||||
MutablePropertySources capturedPropertySources = environment.getPropertySources();
|
||||
// Only copy the default property source(s) and the profiles over from the main
|
||||
// environment (everything else should be pristine, just like it was on startup).
|
||||
for (String name : DEFAULT_PROPERTY_SOURCES) {
|
||||
List<String> propertySourcesToRetain = new ArrayList<>(Arrays.asList(DEFAULT_PROPERTY_SOURCES));
|
||||
if (!CollectionUtils.isEmpty(additionalPropertySourcesToRetain)) {
|
||||
propertySourcesToRetain.addAll(additionalPropertySourcesToRetain);
|
||||
}
|
||||
|
||||
for (String name : propertySourcesToRetain) {
|
||||
if (input.getPropertySources().contains(name)) {
|
||||
if (capturedPropertySources.contains(name)) {
|
||||
capturedPropertySources.replace(name, input.getPropertySources().get(name));
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.Arrays;
|
||||
import org.springframework.boot.Banner;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
|
||||
import org.springframework.cloud.bootstrap.BootstrapApplicationListener;
|
||||
import org.springframework.cloud.bootstrap.BootstrapConfigFileApplicationListener;
|
||||
import org.springframework.cloud.context.scope.refresh.RefreshScope;
|
||||
@@ -37,10 +38,16 @@ import static org.springframework.cloud.util.PropertyUtils.BOOTSTRAP_ENABLED_PRO
|
||||
*/
|
||||
public class LegacyContextRefresher extends ContextRefresher {
|
||||
|
||||
@Deprecated
|
||||
public LegacyContextRefresher(ConfigurableApplicationContext context, RefreshScope scope) {
|
||||
super(context, scope);
|
||||
}
|
||||
|
||||
public LegacyContextRefresher(ConfigurableApplicationContext context, RefreshScope scope,
|
||||
RefreshAutoConfiguration.RefreshProperties properties) {
|
||||
super(context, scope, properties);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateEnvironment() {
|
||||
addConfigFilesToEnvironment();
|
||||
|
||||
Reference in New Issue
Block a user