diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySource.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySource.java index 2abdc3499..0f1158a82 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySource.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySource.java @@ -16,8 +16,10 @@ package com.alibaba.cloud.nacos.client; +import java.util.ArrayList; import java.util.Collections; import java.util.Date; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -63,7 +65,7 @@ public class NacosPropertySource extends MapPropertySource { } NacosPropertySource(List> propertySources, String group, - String dataId, Date timestamp, boolean isRefreshable) { + String dataId, Date timestamp, boolean isRefreshable) { this(group, dataId, getSourceMap(group, dataId, propertySources), timestamp, isRefreshable); } @@ -80,12 +82,32 @@ public class NacosPropertySource extends MapPropertySource { return (Map) propertySource.getSource(); } } - // If it is multiple, it will be returned as it is, and the internal elements - // cannot be directly retrieved, so the user needs to implement the retrieval - // logic by himself - return Collections.singletonMap( - String.join(NacosConfigProperties.COMMAS, dataId, group), - propertySources); + + Map sourceMap = new LinkedHashMap<>(); + List> otherTypePropertySources = new ArrayList<>(); + for (PropertySource propertySource : propertySources) { + if (propertySource == null) { + continue; + } + if (propertySource instanceof MapPropertySource) { + // If the Nacos configuration file uses "---" to separate property name, + // propertySources will be multiple documents, and every document is a map. + // see org.springframework.boot.env.YamlPropertySourceLoader#load + MapPropertySource mapPropertySource = (MapPropertySource) propertySource; + Map source = mapPropertySource.getSource(); + sourceMap.putAll(source); + } else { + otherTypePropertySources.add(propertySource); + } + } + + // Other property sources which is not instanceof MapPropertySource will be put as it is, + // and the internal elements cannot be directly retrieved, + // so the user needs to implement the retrieval logic by himself + if (!otherTypePropertySources.isEmpty()) { + sourceMap.put(String.join(NacosConfigProperties.COMMAS, dataId, group), otherTypePropertySources); + } + return sourceMap; } public String getGroup() { diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosFileExtensionTest.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosFileExtensionTest.java index 229610514..6e2e36504 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosFileExtensionTest.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosFileExtensionTest.java @@ -67,7 +67,7 @@ public class NacosFileExtensionTest { throws Throwable { if ("test-name.yaml".equals(args[0]) && "DEFAULT_GROUP".equals(args[1])) { - return "user:\n name: hello\n age: 12"; + return "user:\n name: hello\n age: 12\n---\nuser:\n gender: male"; } return ""; } @@ -88,6 +88,7 @@ public class NacosFileExtensionTest { Assert.assertEquals(environment.getProperty("user.name"), "hello"); Assert.assertEquals(environment.getProperty("user.age"), "12"); + Assert.assertEquals(environment.getProperty("user.gender"), "male"); } @Configuration