|
17 | 17 | package org.springframework.boot.context.properties;
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
| 20 | +import java.util.HashMap; |
20 | 21 | import java.util.Iterator;
|
21 | 22 | import java.util.Map;
|
22 | 23 |
|
|
29 | 30 | import org.springframework.beans.factory.ListableBeanFactory;
|
30 | 31 | import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
31 | 32 | import org.springframework.beans.factory.config.BeanPostProcessor;
|
| 33 | +import org.springframework.boot.SpringApplication; |
32 | 34 | import org.springframework.boot.bind.PropertiesConfigurationFactory;
|
33 | 35 | import org.springframework.boot.env.PropertySourcesLoader;
|
34 | 36 | import org.springframework.context.ApplicationContext;
|
|
49 | 51 | import org.springframework.core.env.PropertySource;
|
50 | 52 | import org.springframework.core.env.PropertySources;
|
51 | 53 | import org.springframework.core.env.StandardEnvironment;
|
| 54 | +import org.springframework.core.env.SystemEnvironmentPropertySource; |
52 | 55 | import org.springframework.core.io.DefaultResourceLoader;
|
53 | 56 | import org.springframework.core.io.Resource;
|
54 | 57 | import org.springframework.core.io.ResourceLoader;
|
@@ -203,6 +206,13 @@ public void destroy() throws Exception {
|
203 | 206 | }
|
204 | 207 |
|
205 | 208 | private PropertySources deducePropertySources() {
|
| 209 | + String prefix = this.environment |
| 210 | + .getProperty(SpringApplication.PROPERTY_BINDING_PREFIX_PROPERTY, ""); |
| 211 | + if (prefix != null && !"".equals(prefix)) { |
| 212 | + filterSystemEnvironmentPropertySourceByPrefix( |
| 213 | + (ConfigurableEnvironment) this.environment, prefix); |
| 214 | + } |
| 215 | + |
206 | 216 | PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer();
|
207 | 217 | if (configurer != null) {
|
208 | 218 | // Flatten the sources into a single list so they can be iterated
|
@@ -233,6 +243,28 @@ private PropertySourcesPlaceholderConfigurer getSinglePropertySourcesPlaceholder
|
233 | 243 | return null;
|
234 | 244 | }
|
235 | 245 |
|
| 246 | + private void filterSystemEnvironmentPropertySourceByPrefix( |
| 247 | + ConfigurableEnvironment environment, String prefix) { |
| 248 | + final Map<String, Object> environmentProperties = environment.getSystemProperties(); |
| 249 | + final Map<String, Object> filteredSystemProperties = new HashMap<String, Object>( |
| 250 | + environmentProperties.size()); |
| 251 | + |
| 252 | + for (String key : environmentProperties.keySet()) { |
| 253 | + if (key.startsWith(prefix)) { |
| 254 | + filteredSystemProperties.put(key.substring(prefix.length()), |
| 255 | + environmentProperties.get(key)); |
| 256 | + } else { |
| 257 | + filteredSystemProperties.put(key, environmentProperties.get(key)); |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + environment.getPropertySources() |
| 262 | + .replace(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, |
| 263 | + new SystemEnvironmentPropertySource( |
| 264 | + StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, |
| 265 | + filteredSystemProperties)); |
| 266 | + } |
| 267 | + |
236 | 268 | private <T> T getOptionalBean(String name, Class<T> type) {
|
237 | 269 | try {
|
238 | 270 | return this.beanFactory.getBean(name, type);
|
|
0 commit comments