Skip to content

Commit

Permalink
feat: add a config adjust the property source overriden behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
shenhuaxin committed May 24, 2022
1 parent b6dafd2 commit 6d4b5d8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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. <br /> <br />
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6d4b5d8

Please sign in to comment.