PropertySourcesUtils { public static Map<String, Object> getSubProperties(Iterable<PropertySource<?>> propertySources, String prefix) { AbstractEnvironment environment = new AbstractEnvironment() { }; MutablePropertySources mutablePropertySources = environment.getPropertySources(); for (PropertySource<?> source : propertySources) { mutablePropertySources.addLast(source); } return getSubProperties(environment, prefix); }  static Map<String, Object> getSubProperties(Iterable<PropertySource<?>> propertySources, String prefix); static Map<String, Object> getSubProperties(ConfigurableEnvironment environment, String prefix); static String normalizePrefix(String prefix);  }
@Test public void testGetSubProperties() { MutablePropertySources propertySources = new MutablePropertySources(); Map<String, Object> source = new HashMap<String, Object>(); Map<String, Object> source2 = new HashMap<String, Object>(); MapPropertySource propertySource = new MapPropertySource("propertySource", source); MapPropertySource propertySource2 = new MapPropertySource("propertySource2", source2); propertySources.addLast(propertySource); propertySources.addLast(propertySource2); Map<String, Object> result = PropertySourcesUtils.getSubProperties(propertySources, "user"); Assert.assertEquals(Collections.emptyMap(), result); source.put("age", "31"); source.put("user.name", "Mercy"); source.put("user.age", "${age}"); source2.put("user.name", "mercyblitz"); source2.put("user.age", "32"); Map<String, Object> expected = new HashMap<String, Object>(); expected.put("name", "Mercy"); expected.put("age", "31"); result = PropertySourcesUtils.getSubProperties(propertySources, "user"); Assert.assertEquals(expected, result); result = PropertySourcesUtils.getSubProperties(propertySources, ""); Assert.assertEquals(Collections.emptyMap(), result); result = PropertySourcesUtils.getSubProperties(propertySources, "no-exists"); Assert.assertEquals(Collections.emptyMap(), result); }