Skip to content

Commit d8d156b

Browse files
committed
Improve documentation on EnvironmentPostProcessor
Closes gh-9617
1 parent 606bc77 commit d8d156b

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ refreshed using `EnvironmentPostProcessor`. Each implementation should be regist
115115
org.springframework.boot.env.EnvironmentPostProcessor=com.example.YourEnvironmentPostProcessor
116116
----
117117

118+
The implementation can load arbitrary files and add them to the `Environment`. For
119+
instance, this example loads a YAML configuration file from the classpath:
120+
121+
122+
[source,java,indent=0]
123+
----
124+
include::{code-examples}/context/EnvironmentPostProcessorExample.java[tag=example]
125+
----
126+
127+
TIP: The `Environment` will already have been prepared with all the usual property sources
128+
that Spring Boot loads by default. It is therefore possible to get the location of the
129+
file from the environment. This example adds the `custom-resource` property source at the
130+
end of the list so that a key defined in any of the usual other locations takes
131+
precedence. A custom implementation may obviously defines another order.
132+
133+
NOTE: While using `@PropertySource` on your `@SpringBootApplication` seems convenient and
134+
easy enough to load a custom resource in the `Environment`, we do not recommend it as
135+
Spring Boot prepares the `Environment` before the `ApplicationContext` is refreshed. Any
136+
key defined via `@PropertySource` will be loaded too late to have any effect on
137+
auto-configuration.
118138

119139

120140
[[howto-build-an-application-context-hierarchy]]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2012-2017 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.context;
18+
19+
import java.io.IOException;
20+
21+
import org.springframework.boot.SpringApplication;
22+
import org.springframework.boot.env.EnvironmentPostProcessor;
23+
import org.springframework.boot.env.YamlPropertySourceLoader;
24+
import org.springframework.core.env.ConfigurableEnvironment;
25+
import org.springframework.core.env.PropertySource;
26+
import org.springframework.core.io.ClassPathResource;
27+
import org.springframework.core.io.Resource;
28+
29+
/**
30+
* An {@link EnvironmentPostProcessor} example that loads a YAML file.
31+
*
32+
* @author Stephane Nicoll
33+
*/
34+
// tag::example[]
35+
public class EnvironmentPostProcessorExample
36+
implements EnvironmentPostProcessor {
37+
38+
private final YamlPropertySourceLoader loader
39+
= new YamlPropertySourceLoader();
40+
41+
@Override
42+
public void postProcessEnvironment(ConfigurableEnvironment environment,
43+
SpringApplication application) {
44+
Resource path = new ClassPathResource("com/example/myapp/config.yml");
45+
PropertySource<?> propertySource = loadYaml(path);
46+
environment.getPropertySources().addLast(propertySource);
47+
}
48+
49+
private PropertySource<?> loadYaml(Resource path) {
50+
if (!path.exists()) {
51+
throw new IllegalArgumentException("Resource " + path
52+
+ " does not exist");
53+
}
54+
try {
55+
return this.loader.load("custom-resource", path, null);
56+
}
57+
catch (IOException ex) {
58+
throw new IllegalStateException("Failed to load yaml configuration "
59+
+ "from " + path, ex);
60+
}
61+
}
62+
63+
}
64+
// end::example[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2017 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.context;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.boot.SpringApplication;
22+
import org.springframework.core.env.StandardEnvironment;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link EnvironmentPostProcessorExample}.
28+
*
29+
* @author Stephane Nicoll
30+
*/
31+
public class EnvironmentPostProcessorExampleTests {
32+
33+
private final StandardEnvironment environment = new StandardEnvironment();
34+
35+
@Test
36+
public void applyEnvironmentPostProcessor() {
37+
assertThat(this.environment.containsProperty("test.foo.bar")).isFalse();
38+
new EnvironmentPostProcessorExample().postProcessEnvironment(
39+
this.environment, new SpringApplication());
40+
assertThat(this.environment.containsProperty("test.foo.bar")).isTrue();
41+
assertThat(this.environment.getProperty("test.foo.bar")).isEqualTo("value");
42+
}
43+
44+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test:
2+
foo:
3+
bar: value

0 commit comments

Comments
 (0)