From 792f2539c3f2d5aa5a69e6c33a84288da0a75304 Mon Sep 17 00:00:00 2001 From: liaozan <378024053@qq.com> Date: Sat, 1 Jul 2023 01:18:34 +0800 Subject: [PATCH] Replace CompositePropertySource with single OrderedMapPropertySource --- .../apollo/ConfigurablePropertiesLoader.java | 51 +++++++++---------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java index b263646..7f2ae01 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java @@ -15,7 +15,7 @@ import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ApplicationEventMulticaster; import org.springframework.context.event.SimpleApplicationEventMulticaster; -import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.ResolvableType; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.util.ClassUtils; @@ -30,18 +30,13 @@ import static org.springframework.core.io.support.SpringFactoriesLoader.loadFact */ class ConfigurablePropertiesLoader { - /** - * the name of properties propertySource - */ - public static final String PROPERTIES_PROPERTY_SOURCE = "ConfigurablePropertiesPropertySource"; - private final Log log; private final DeferredLogFactory deferredLogFactory; ConfigurablePropertiesLoader(DeferredLogFactory deferredLogFactory) { - this.log = deferredLogFactory.getLog(ConfigurablePropertiesLoader.class); this.deferredLogFactory = deferredLogFactory; + this.log = deferredLogFactory.getLog(ConfigurablePropertiesLoader.class); } void load(ConfigurableEnvironment environment, SpringApplication application) { @@ -52,34 +47,36 @@ class ConfigurablePropertiesLoader { } ApplicationEventMulticaster eventMulticaster = createEventMulticaster(application.getListeners()); + boolean remoteFirst = ApolloProperties.get(environment).isRemoteFirst(); - ApolloProperties apolloProperties = ApolloProperties.get(environment); - - // MUST NOT use CachedCompositePropertySource, because propertyNames will be cached when property loading - CompositePropertySource compositePropertySource = new CompositePropertySource(PROPERTIES_PROPERTY_SOURCE); - if (apolloProperties.isRemoteFirst()) { - environment.getPropertySources().addFirst(compositePropertySource); - } else { - environment.getPropertySources().addLast(compositePropertySource); - } - - configurableProperties.forEach(properties -> { - String namespace = properties.getNamespace(); - Config config = ConfigService.getConfig(namespace); - OrderedMapPropertySource propertySource = ConfigUtils.toPropertySource(namespace, config); + configurableProperties.parallelStream().forEach(properties -> { + OrderedMapPropertySource propertySource = loadFromRemote(environment, remoteFirst, properties.getNamespace()); if (propertySource == null) { - log.warn("No configuration properties loaded under namespace: " + namespace); return; } - // early add to environment to support properties bind - compositePropertySource.addPropertySource(propertySource); - // resolve any placeHolders - ConfigUtils.resolvePlaceHolders(environment, propertySource); // multicast event - eventMulticaster.multicastEvent(createEvent(environment, application, propertySource, properties)); + ConfigLoadedEvent event = createEvent(environment, application, propertySource, properties); + eventMulticaster.multicastEvent(event, ResolvableType.forClass(event.getClass())); }); } + private OrderedMapPropertySource loadFromRemote(ConfigurableEnvironment environment, boolean remoteFirst, String namespace) { + Config config = ConfigService.getConfig(namespace); + OrderedMapPropertySource propertySource = ConfigUtils.toPropertySource(namespace, config); + if (propertySource == null) { + log.warn("No configuration properties loaded under namespace: " + namespace); + return null; + } + if (remoteFirst) { + environment.getPropertySources().addFirst(propertySource); + } else { + environment.getPropertySources().addLast(propertySource); + } + // resolve any placeHolders + ConfigUtils.resolvePlaceHolders(environment, propertySource); + return propertySource; + } + private ConfigLoadedEvent createEvent(ConfigurableEnvironment environment, SpringApplication application, OrderedMapPropertySource propertySource, ConfigurableProperties properties) { ConfigurableProperties boundProperties = properties.bind(environment); -- GitLab