Skip to content

Commit e23f628

Browse files
committed
feat: add a config adjust the property source overriden behavior
1 parent 698cfba commit e23f628

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializer.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.core.Ordered;
4040
import org.springframework.core.env.CompositePropertySource;
4141
import org.springframework.core.env.ConfigurableEnvironment;
42+
import org.springframework.core.env.StandardEnvironment;
4243

4344
/**
4445
* Initialize apollo system properties and inject the Apollo config in Spring Boot bootstrap phase
@@ -140,7 +141,12 @@ protected void initialize(ConfigurableEnvironment environment) {
140141

141142
composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config));
142143
}
143-
144+
if (environment.getPropertySources().contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
145+
if (!isOverrideSystemProperties(environment)) {
146+
environment.getPropertySources().addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, composite);
147+
return;
148+
}
149+
}
144150
environment.getPropertySources().addFirst(composite);
145151
}
146152

@@ -215,4 +221,9 @@ public int getOrder() {
215221
public void setOrder(int order) {
216222
this.order = order;
217223
}
224+
225+
226+
private boolean isOverrideSystemProperties(ConfigurableEnvironment environment) {
227+
return environment.getProperty(PropertySourcesConstants.APOLLO_OVERRIDE_SYSTEM_PROPERTIES, Boolean.class, true);
228+
}
218229
}

apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ public interface PropertySourcesConstants {
2222
String APOLLO_BOOTSTRAP_ENABLED = "apollo.bootstrap.enabled";
2323
String APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED = "apollo.bootstrap.eagerLoad.enabled";
2424
String APOLLO_BOOTSTRAP_NAMESPACES = "apollo.bootstrap.namespaces";
25+
String APOLLO_OVERRIDE_SYSTEM_PROPERTIES = "apollo.overrideSystemProperties";
2526
}

apollo-client/src/test/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializerTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@
2727
import com.ctrip.framework.apollo.spring.config.CachedCompositePropertySource;
2828
import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants;
2929
import com.ctrip.framework.apollo.util.ConfigUtil;
30+
import jdk.jfr.Event;
3031
import org.junit.After;
3132
import org.junit.Before;
3233
import org.junit.Test;
3334
import org.springframework.core.env.ConfigurableEnvironment;
3435
import org.springframework.core.env.MutablePropertySources;
36+
import org.springframework.core.env.PropertiesPropertySource;
37+
import org.springframework.core.env.StandardEnvironment;
38+
39+
import java.util.Properties;
3540

3641
public class ApolloApplicationContextInitializerTest {
3742

@@ -143,4 +148,24 @@ public void testPropertyNamesCacheEnabled() {
143148
assertTrue(propertySources.contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME));
144149
assertTrue(propertySources.iterator().next() instanceof CachedCompositePropertySource);
145150
}
151+
152+
@Test
153+
public void testOverrideSystemProperties() {
154+
Properties properties = new Properties();
155+
properties.setProperty("server.port", "8080");
156+
ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class);
157+
158+
MutablePropertySources propertySources = new MutablePropertySources();
159+
propertySources.addLast(new PropertiesPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, properties));
160+
161+
when(environment.getPropertySources()).thenReturn(propertySources);
162+
when(environment.getProperty(PropertySourcesConstants.APOLLO_OVERRIDE_SYSTEM_PROPERTIES, Boolean.class, true)).thenReturn(false);
163+
when(environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES,
164+
ConfigConsts.NAMESPACE_APPLICATION)).thenReturn("");
165+
166+
apolloApplicationContextInitializer.initialize(environment);
167+
168+
assertTrue(propertySources.contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME));
169+
assertEquals(propertySources.iterator().next().getName(), StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
170+
}
146171
}

0 commit comments

Comments
 (0)