diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesProcessor.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesProcessor.java index 86fb2eaa2af..6328b3c1b4d 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesProcessor.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesProcessor.java @@ -40,11 +40,7 @@ import org.springframework.context.EnvironmentAware; import org.springframework.core.Ordered; import org.springframework.core.PriorityOrdered; -import org.springframework.core.env.CompositePropertySource; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.Environment; -import org.springframework.core.env.MutablePropertySources; -import org.springframework.core.env.PropertySource; +import org.springframework.core.env.*; /** * Apollo Property Sources processor for Spring Annotation Based Application.

@@ -110,12 +106,20 @@ private void initializePropertySources() { if (environment.getPropertySources() .contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) { - // ensure ApolloBootstrapPropertySources is still the first - ensureBootstrapPropertyPrecedence(environment); + if (!isOverrideSystemProperties(environment)) { + // ensure ApolloBootstrapPropertySources is still the first + ensureBootstrapPropertyPrecedence(environment); + } environment.getPropertySources() .addAfter(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME, composite); } else { + if (environment.getPropertySources().contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) { + if (!isOverrideSystemProperties(environment)) { + environment.getPropertySources().addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, composite); + return; + } + } environment.getPropertySources().addFirst(composite); } } @@ -149,6 +153,10 @@ private void initializeAutoUpdatePropertiesFeature(ConfigurableListableBeanFacto } } + private Boolean isOverrideSystemProperties(ConfigurableEnvironment environment) { + return environment.getProperty(PropertySourcesConstants.APOLLO_OVERRIDE_SYSTEM_PROPERTIES, Boolean.class, true); + } + @Override public void setEnvironment(Environment environment) { //it is safe enough to cast as all known environment is derived from ConfigurableEnvironment diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializerTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializerTest.java index 658ca7e4ef6..d95abd05465 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializerTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializerTest.java @@ -27,7 +27,6 @@ import com.ctrip.framework.apollo.spring.config.CachedCompositePropertySource; import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants; import com.ctrip.framework.apollo.util.ConfigUtil; -import jdk.jfr.Event; import org.junit.After; import org.junit.Before; import org.junit.Test;