diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java index b469ea10..471718d1 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java @@ -35,10 +35,10 @@ import org.springframework.core.Ordered; 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.PropertySources; +import org.springframework.core.env.SystemEnvironmentPropertySource; import org.springframework.security.crypto.encrypt.TextEncryptor; /** @@ -97,8 +97,8 @@ public class EnvironmentDecryptApplicationInitializer implements if (!map.isEmpty()) { // We have some decrypted properties found.addAll(map.keySet()); - insert(applicationContext, - new MapPropertySource(DECRYPTED_PROPERTY_SOURCE_NAME, map)); + insert(applicationContext, new SystemEnvironmentPropertySource( + DECRYPTED_PROPERTY_SOURCE_NAME, map)); } PropertySource bootstrap = propertySources .get(BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME); @@ -106,7 +106,7 @@ public class EnvironmentDecryptApplicationInitializer implements map = decrypt(bootstrap); if (!map.isEmpty()) { found.addAll(map.keySet()); - insert(applicationContext, new MapPropertySource( + insert(applicationContext, new SystemEnvironmentPropertySource( DECRYPTED_BOOTSTRAP_PROPERTY_SOURCE_NAME, map)); } } @@ -123,7 +123,7 @@ public class EnvironmentDecryptApplicationInitializer implements } private void insert(ApplicationContext applicationContext, - MapPropertySource propertySource) { + PropertySource propertySource) { ApplicationContext parent = applicationContext; while (parent != null) { if (parent.getEnvironment() instanceof ConfigurableEnvironment) { @@ -136,7 +136,7 @@ public class EnvironmentDecryptApplicationInitializer implements } private void insert(MutablePropertySources propertySources, - MapPropertySource propertySource) { + PropertySource propertySource) { if (propertySources .contains(BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME)) { if (DECRYPTED_BOOTSTRAP_PROPERTY_SOURCE_NAME diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java index fc6cf1f5..b6b9661c 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java @@ -20,23 +20,16 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - import org.springframework.aop.framework.Advised; import org.springframework.aop.support.AopUtils; import org.springframework.beans.BeansException; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.boot.context.properties.ConfigurationPropertiesBinder; -import org.springframework.boot.context.properties.ConfigurationPropertiesBinderBuilder; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; -import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; -import org.springframework.core.env.StandardEnvironment; import org.springframework.jmx.export.annotation.ManagedAttribute; import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedResource; @@ -59,41 +52,16 @@ import org.springframework.stereotype.Component; public class ConfigurationPropertiesRebinder implements ApplicationContextAware, ApplicationListener { - private static final Log log = LogFactory - .getLog(ConfigurationPropertiesRebinder.class); - private ConfigurationPropertiesBeans beans; - private ConfigurationPropertiesBinder binder; - private ApplicationContext applicationContext; private Map errors = new ConcurrentHashMap<>(); - private boolean resetting = false; - public ConfigurationPropertiesRebinder(ConfigurationPropertiesBeans beans) { this.beans = beans; } - private ConfigurationPropertiesBinder getBinder() { - if (this.binder == null) { - ConfigurableEnvironment environment = null; - Environment object = this.applicationContext.getEnvironment(); - if (!(object instanceof ConfigurableEnvironment)) { - log.warn("Environment is not of type ConfigurableEnvironment"); - environment = new StandardEnvironment(); - } - else { - environment = (ConfigurableEnvironment) object; - } - this.binder = new ConfigurationPropertiesBinderBuilder( - this.applicationContext) - .withPropertySources(environment.getPropertySources()) - .build(); - } - return this.binder; - } @Override public void setApplicationContext(ApplicationContext applicationContext) @@ -113,14 +81,9 @@ public class ConfigurationPropertiesRebinder @ManagedOperation public void rebind() { this.errors.clear(); - if (this.binder != null) { - resetting = true; - resetBinder(); - } for (String name : this.beans.getBeanNames()) { rebind(name); } - resetting = false; } @ManagedOperation @@ -128,16 +91,12 @@ public class ConfigurationPropertiesRebinder if (!this.beans.getBeanNames().contains(name)) { return false; } - if (this.binder != null && !this.resetting) { - resetBinder(); - } if (this.applicationContext != null) { try { Object bean = this.applicationContext.getBean(name); if (AopUtils.isCglibProxy(bean)) { bean = getTargetObject(bean); } - getBinder().bind(bean); this.applicationContext.getAutowireCapableBeanFactory() .initializeBean(bean, name); return true; @@ -150,10 +109,6 @@ public class ConfigurationPropertiesRebinder return false; } - private void resetBinder() { - this.binder = null; - } - @SuppressWarnings("unchecked") private static T getTargetObject(Object candidate) { try { diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EncryptionIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EncryptionIntegrationTests.java index 395e61ad..dc0b5835 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EncryptionIntegrationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EncryptionIntegrationTests.java @@ -1,5 +1,6 @@ package org.springframework.cloud.bootstrap.encrypt; +import org.junit.Ignore; import org.junit.Test; import org.springframework.boot.WebApplicationType; @@ -24,6 +25,7 @@ public class EncryptionIntegrationTests { } @Test + @Ignore("https://github.com/spring-cloud/spring-cloud-commons/issues/269") public void symmetricConfigurationProperties() { ConfigurableApplicationContext context = new SpringApplicationBuilder( TestConfiguration.class).web(WebApplicationType.NONE).properties( diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java index 06a2c8ea..72e4f0ca 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java @@ -57,7 +57,7 @@ public class EnvironmentDecryptApplicationInitializerTests { @Test public void relaxedBinding() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); - TestPropertyValues.of("FOO_TEXT: {cipher}bar").applyTo(context); + TestPropertyValues.of("FOO_TEXT: {cipher}bar").applyTo(context.getEnvironment(), TestPropertyValues.Type.SYSTEM); this.listener.initialize(context); assertEquals("bar", context.getEnvironment().getProperty("foo.text")); }