diff --git a/src/main/java/com/alibaba/spring/util/PropertySourcesUtils.java b/src/main/java/com/alibaba/spring/util/PropertySourcesUtils.java index 8c258a0..f3973b1 100644 --- a/src/main/java/com/alibaba/spring/util/PropertySourcesUtils.java +++ b/src/main/java/com/alibaba/spring/util/PropertySourcesUtils.java @@ -1,9 +1,8 @@ package com.alibaba.spring.util; -import org.springframework.core.env.EnumerablePropertySource; -import org.springframework.core.env.PropertySource; -import org.springframework.core.env.PropertySources; +import org.springframework.core.env.*; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; @@ -27,8 +26,34 @@ public abstract class PropertySourcesUtils { */ public static Map getSubProperties(Iterable> propertySources, String prefix) { + // Non-Extension AbstractEnvironment + AbstractEnvironment environment = new AbstractEnvironment() { + }; + + MutablePropertySources mutablePropertySources = environment.getPropertySources(); + + for (PropertySource source : propertySources) { + mutablePropertySources.addLast(source); + } + + return getSubProperties(environment, prefix); + + } + + /** + * Get Sub {@link Properties} + * + * @param environment {@link ConfigurableEnvironment} + * @param prefix the prefix of property name + * @return Map + * @see Properties + */ + public static Map getSubProperties(ConfigurableEnvironment environment, String prefix) { + Map subProperties = new LinkedHashMap(); + MutablePropertySources propertySources = environment.getPropertySources(); + String normalizedPrefix = normalizePrefix(prefix); for (PropertySource source : propertySources) { @@ -38,6 +63,10 @@ public static Map getSubProperties(Iterable> p String subName = name.substring(normalizedPrefix.length()); if (!subProperties.containsKey(subName)) { // take first one Object value = source.getProperty(name); + if (value instanceof String) { + // Resolve placeholder + value = environment.resolvePlaceholders((String) value); + } subProperties.put(subName, value); } } @@ -45,7 +74,7 @@ public static Map getSubProperties(Iterable> p } } - return subProperties; + return Collections.unmodifiableMap(subProperties); } diff --git a/src/test/java/com/alibaba/spring/util/PropertySourcesUtilsTest.java b/src/test/java/com/alibaba/spring/util/PropertySourcesUtilsTest.java index 058aa72..8aecfe7 100644 --- a/src/test/java/com/alibaba/spring/util/PropertySourcesUtilsTest.java +++ b/src/test/java/com/alibaba/spring/util/PropertySourcesUtilsTest.java @@ -24,18 +24,24 @@ public void testGetSubProperties() { MutablePropertySources propertySources = new MutablePropertySources(); Map source = new HashMap(); + Map source2 = new HashMap(); - MapPropertySource propertySource = new MapPropertySource("test", source); + MapPropertySource propertySource = new MapPropertySource("propertySource", source); + MapPropertySource propertySource2 = new MapPropertySource("propertySource2", source2); - propertySources.addFirst(propertySource); - propertySources.addFirst(propertySource); + propertySources.addLast(propertySource); + propertySources.addLast(propertySource2); Map result = PropertySourcesUtils.getSubProperties(propertySources, "user"); Assert.assertEquals(Collections.emptyMap(), result); + source.put("age", "31"); source.put("user.name", "Mercy"); - source.put("user.age", "31"); + source.put("user.age", "${age}"); + + source2.put("user.name", "mercyblitz"); + source2.put("user.age", "32"); Map expected = new HashMap(); expected.put("name", "Mercy");