|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.util.Collections;
|
| 21 | +import java.util.HashMap; |
21 | 22 | import java.util.Iterator;
|
22 | 23 | import java.util.List;
|
23 | 24 | import java.util.Map;
|
|
32 | 33 | import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
33 | 34 | import org.springframework.beans.factory.annotation.Autowired;
|
34 | 35 | import org.springframework.beans.factory.config.BeanPostProcessor;
|
| 36 | +import org.springframework.boot.SpringApplication; |
35 | 37 | import org.springframework.boot.bind.PropertiesConfigurationFactory;
|
36 | 38 | import org.springframework.boot.env.PropertySourcesLoader;
|
37 | 39 | import org.springframework.context.ApplicationContext;
|
|
55 | 57 | import org.springframework.core.env.PropertySource;
|
56 | 58 | import org.springframework.core.env.PropertySources;
|
57 | 59 | import org.springframework.core.env.StandardEnvironment;
|
| 60 | +import org.springframework.core.env.SystemEnvironmentPropertySource; |
58 | 61 | import org.springframework.core.io.DefaultResourceLoader;
|
59 | 62 | import org.springframework.core.io.Resource;
|
60 | 63 | import org.springframework.core.io.ResourceLoader;
|
@@ -243,6 +246,13 @@ private void freeLocalValidator() {
|
243 | 246 | }
|
244 | 247 |
|
245 | 248 | private PropertySources deducePropertySources() {
|
| 249 | + String prefix = this.environment |
| 250 | + .getProperty(SpringApplication.PROPERTY_BINDING_PREFIX_PROPERTY, ""); |
| 251 | + if (prefix != null && !"".equals(prefix)) { |
| 252 | + filterSystemEnvironmentPropertySourceByPrefix( |
| 253 | + (ConfigurableEnvironment) this.environment, prefix); |
| 254 | + } |
| 255 | + |
246 | 256 | PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer();
|
247 | 257 | if (configurer != null) {
|
248 | 258 | // Flatten the sources into a single list so they can be iterated
|
@@ -271,6 +281,28 @@ private PropertySourcesPlaceholderConfigurer getSinglePropertySourcesPlaceholder
|
271 | 281 | return null;
|
272 | 282 | }
|
273 | 283 |
|
| 284 | + private void filterSystemEnvironmentPropertySourceByPrefix( |
| 285 | + ConfigurableEnvironment environment, String prefix) { |
| 286 | + final Map<String, Object> environmentProperties = environment.getSystemProperties(); |
| 287 | + final Map<String, Object> filteredSystemProperties = new HashMap<String, Object>( |
| 288 | + environmentProperties.size()); |
| 289 | + |
| 290 | + for (String key : environmentProperties.keySet()) { |
| 291 | + if (key.startsWith(prefix)) { |
| 292 | + filteredSystemProperties.put(key.substring(prefix.length()), |
| 293 | + environmentProperties.get(key)); |
| 294 | + } else { |
| 295 | + filteredSystemProperties.put(key, environmentProperties.get(key)); |
| 296 | + } |
| 297 | + } |
| 298 | + |
| 299 | + environment.getPropertySources() |
| 300 | + .replace(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, |
| 301 | + new SystemEnvironmentPropertySource( |
| 302 | + StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, |
| 303 | + filteredSystemProperties)); |
| 304 | + } |
| 305 | + |
274 | 306 | private <T> T getOptionalBean(String name, Class<T> type) {
|
275 | 307 | try {
|
276 | 308 | return this.beanFactory.getBean(name, type);
|
|
0 commit comments