diff --git a/CHANGES.md b/CHANGES.md index 7f3f7a1f..c57e1d8e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ Apollo Java 2.1.0 * [Add a new API to load items with pagination](https://github.com/apolloconfig/apollo/pull/4468) * [fix openapi item with url illegalKey 400 error](https://github.com/apolloconfig/apollo/pull/4549) * [Add overloaded shortcut method to register BeanDefinition](https://github.com/apolloconfig/apollo/pull/4574) +* [Fix ApolloBootstrapPropertySources precedence issue](https://github.com/apolloconfig/apollo-java/pull/3) ------------------ All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/1?closed=1) \ No newline at end of file diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializer.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializer.java index fa37484f..17890055 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializer.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializer.java @@ -25,6 +25,7 @@ import com.ctrip.framework.apollo.spring.config.CachedCompositePropertySource; import com.ctrip.framework.apollo.spring.config.ConfigPropertySourceFactory; import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants; +import com.ctrip.framework.apollo.spring.util.PropertySourcesUtil; import com.ctrip.framework.apollo.spring.util.SpringInjector; import com.ctrip.framework.apollo.util.ConfigUtil; import com.google.common.base.Splitter; @@ -119,10 +120,14 @@ public void initialize(ConfigurableApplicationContext context) { * @param environment */ protected void initialize(ConfigurableEnvironment environment) { - + final ConfigUtil configUtil = ApolloInjector.getInstance(ConfigUtil.class); if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) { //already initialized, replay the logs that were printed before the logging system was initialized DeferredLogger.replayTo(); + if (configUtil.isOverrideSystemProperties()) { + // ensure ApolloBootstrapPropertySources is still the first + PropertySourcesUtil.ensureBootstrapPropertyPrecedence(environment); + } return; } @@ -131,7 +136,6 @@ protected void initialize(ConfigurableEnvironment environment) { List namespaceList = NAMESPACE_SPLITTER.splitToList(namespaces); CompositePropertySource composite; - final ConfigUtil configUtil = ApolloInjector.getInstance(ConfigUtil.class); if (configUtil.isPropertyNamesCacheEnabled()) { composite = new CachedCompositePropertySource(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME); } else { 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 2e26a153..a6363948 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 @@ -20,8 +20,8 @@ import com.ctrip.framework.apollo.ConfigChangeListener; import com.ctrip.framework.apollo.ConfigService; import com.ctrip.framework.apollo.build.ApolloInjector; -import com.ctrip.framework.apollo.core.ApolloClientSystemConsts; import com.ctrip.framework.apollo.spring.events.ApolloConfigChangeEvent; +import com.ctrip.framework.apollo.spring.util.PropertySourcesUtil; import com.ctrip.framework.apollo.spring.util.SpringInjector; import com.ctrip.framework.apollo.util.ConfigUtil; import com.google.common.collect.ImmutableSortedSet; @@ -109,7 +109,7 @@ private void initializePropertySources() { if (configUtil.isOverrideSystemProperties()) { // ensure ApolloBootstrapPropertySources is still the first - ensureBootstrapPropertyPrecedence(environment); + PropertySourcesUtil.ensureBootstrapPropertyPrecedence(environment); } environment.getPropertySources() @@ -125,21 +125,6 @@ private void initializePropertySources() { } } - private void ensureBootstrapPropertyPrecedence(ConfigurableEnvironment environment) { - MutablePropertySources propertySources = environment.getPropertySources(); - - PropertySource bootstrapPropertySource = propertySources - .get(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME); - - // not exists or already in the first place - if (bootstrapPropertySource == null || propertySources.precedenceOf(bootstrapPropertySource) == 0) { - return; - } - - propertySources.remove(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME); - propertySources.addFirst(bootstrapPropertySource); - } - private void initializeAutoUpdatePropertiesFeature(ConfigurableListableBeanFactory beanFactory) { if (!AUTO_UPDATE_INITIALIZED_BEAN_FACTORIES.add(beanFactory)) { return; diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/util/PropertySourcesUtil.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/util/PropertySourcesUtil.java new file mode 100644 index 00000000..dcc95d9a --- /dev/null +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/util/PropertySourcesUtil.java @@ -0,0 +1,49 @@ +/* + * Copyright 2022 Apollo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package com.ctrip.framework.apollo.spring.util; + +import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; + +/** + * @author Gallant + */ +public class PropertySourcesUtil { + + + /** + * Ensure ApolloBootstrapPropertySources is still the first + * @param environment : Ensure ApolloBootstrapPropertySources is still the first in the environment + */ + public static void ensureBootstrapPropertyPrecedence(ConfigurableEnvironment environment) { + MutablePropertySources propertySources = environment.getPropertySources(); + + PropertySource bootstrapPropertySource = propertySources + .get(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME); + + // not exists or already in the first place + if (bootstrapPropertySource == null || propertySources.precedenceOf(bootstrapPropertySource) == 0) { + return; + } + + propertySources.remove(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME); + propertySources.addFirst(bootstrapPropertySource); + } + +}