Skip to content

Commit f0bfecd

Browse files
author
Phillip Webb
committed
Refactor PropertySource support
Locate PropertySourcesLoaders using SpringFactoriesLoader and refactor the interface to expose file extensions and support 'profiles' within documents. Rework ConfigFileApplicationListener for consistent profile loading. Profiles are now loaded in a consistent order for both profile specific files, and contained profile documents (i.e. YAML sub-sections). Also update ConfigFileApplicationListener so that it no longer directly processes @ProperySource annotations. Instead the standard Spring ConfigurationClassPostProcessor will insert @propertysource items with ConfigFileApplicationListener later re-ordering them. The SpringApplication can no longer be configured using @ProperySource annotations, however, application.properties may still be used. Fixes spring-projectsgh-322
1 parent 06494e0 commit f0bfecd

20 files changed

+806
-795
lines changed

spring-boot/src/main/java/org/springframework/boot/cloudfoundry/VcapApplicationListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public class VcapApplicationListener implements
9898
private static final String VCAP_SERVICES = "VCAP_SERVICES";
9999

100100
// Before ConfigFileApplicationListener so values there can use these ones
101-
private int order = ConfigFileApplicationListener.DEFAULT_CONFIG_LISTENER_ORDER - 1;;
101+
private int order = ConfigFileApplicationListener.DEFAULT_ORDER - 1;;
102102

103103
private final JsonParser parser = JsonParserFactory.getJsonParser();
104104

spring-boot/src/main/java/org/springframework/boot/config/ConfigFileApplicationListener.java

Lines changed: 245 additions & 347 deletions
Large diffs are not rendered by default.

spring-boot/src/main/java/org/springframework/boot/config/DefaultPropertySourceLoadersFactory.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

spring-boot/src/main/java/org/springframework/boot/config/PropertiesPropertySourceLoader.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

spring-boot/src/main/java/org/springframework/boot/config/PropertySourceLoadersFactory.java

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.config;
18+
19+
import java.util.Random;
20+
21+
import org.springframework.core.env.ConfigurableEnvironment;
22+
import org.springframework.core.env.PropertySource;
23+
import org.springframework.core.env.StandardEnvironment;
24+
import org.springframework.util.DigestUtils;
25+
26+
/**
27+
* {@link PropertySource} that returns a random value for any property that starts with
28+
* {@literal "random."}. Return a {@code byte[]} unless the property name ends with
29+
* {@literal ".int} or {@literal ".long"}.
30+
*
31+
* @author Dave Syer
32+
*/
33+
public class RandomValuePropertySource extends PropertySource<Random> {
34+
35+
public RandomValuePropertySource(String name) {
36+
super(name, new Random());
37+
}
38+
39+
@Override
40+
public Object getProperty(String name) {
41+
if (!name.startsWith("random.")) {
42+
return null;
43+
}
44+
if (name.endsWith("int")) {
45+
return getSource().nextInt();
46+
}
47+
if (name.endsWith("long")) {
48+
return getSource().nextLong();
49+
}
50+
byte[] bytes = new byte[32];
51+
getSource().nextBytes(bytes);
52+
return DigestUtils.md5DigestAsHex(bytes);
53+
}
54+
55+
public static void addToEnvironment(ConfigurableEnvironment environment) {
56+
environment.getPropertySources().addAfter(
57+
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
58+
new RandomValuePropertySource("random"));
59+
}
60+
61+
}

spring-boot/src/main/java/org/springframework/boot/config/YamlPropertySourceLoader.java

Lines changed: 0 additions & 105 deletions
This file was deleted.

spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.context.properties;
1818

19+
import java.io.IOException;
20+
1921
import org.springframework.beans.BeansException;
2022
import org.springframework.beans.factory.BeanCreationException;
2123
import org.springframework.beans.factory.BeanFactory;
@@ -26,9 +28,7 @@
2628
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2729
import org.springframework.beans.factory.config.BeanPostProcessor;
2830
import org.springframework.boot.bind.PropertiesConfigurationFactory;
29-
import org.springframework.boot.config.PropertiesPropertySourceLoader;
30-
import org.springframework.boot.config.PropertySourceLoader;
31-
import org.springframework.boot.config.YamlPropertySourceLoader;
31+
import org.springframework.boot.env.PropertySourcesLoader;
3232
import org.springframework.context.ApplicationContext;
3333
import org.springframework.context.ApplicationContextAware;
3434
import org.springframework.context.ConfigurableApplicationContext;
@@ -332,26 +332,22 @@ private Validator determineValidator(Object bean) {
332332
return this.validator;
333333
}
334334

335-
private PropertySources loadPropertySources(String[] path) {
336-
MutablePropertySources propertySources = new MutablePropertySources();
337-
PropertySourceLoader[] loaders = {
338-
new PropertiesPropertySourceLoader(),
339-
YamlPropertySourceLoader.springProfileAwareLoader(this.environment
340-
.getActiveProfiles()) };
341-
for (String location : path) {
342-
location = this.environment.resolvePlaceholders(location);
343-
Resource resource = this.resourceLoader.getResource(location);
344-
if (resource != null && resource.exists()) {
345-
for (PropertySourceLoader loader : loaders) {
346-
if (loader.supports(resource)) {
347-
PropertySource<?> propertySource = loader.load(
348-
resource.getDescription(), resource);
349-
propertySources.addFirst(propertySource);
350-
}
335+
private PropertySources loadPropertySources(String[] locations) {
336+
try {
337+
PropertySourcesLoader loader = new PropertySourcesLoader();
338+
for (String location : locations) {
339+
Resource resource = this.resourceLoader.getResource(this.environment
340+
.resolvePlaceholders(location));
341+
for (String profile : this.environment.getActiveProfiles()) {
342+
loader.load(resource, null, profile);
351343
}
344+
loader.load(resource, null, null);
352345
}
346+
return loader.getPropertySources();
347+
}
348+
catch (IOException ex) {
349+
throw new IllegalStateException(ex);
353350
}
354-
return propertySources;
355351
}
356352

357353
private ConversionService getDefaultConversionService() {

0 commit comments

Comments
 (0)