Add Environment to ProeprtySourceLocator.locate()

In this way each contexet in a hierarchy can in principle have a different
set of property sources
This commit is contained in:
Dave Syer
2014-10-30 11:43:00 +00:00
parent 010d29c124
commit 9efae007fa
11 changed files with 381 additions and 186 deletions

View File

@@ -352,12 +352,15 @@ interface, or with the `SpringApplicationBuilder` convenience methods
(`parent()`, `child()` and `sibling()`). Note that the
`SpringApplicationBuilder` allows you to share an `Environment`
amongst the whole hierarchy, but that is not the default. Thus,
normally you expect to see differences between different levels in the
hierarchy, and sibling contexts in particular do not need to have the
same profiles or property sources, even though they will share common
things with their parent. Every context in the hierarchy will have its
own "bootstrap" property source (possibly empty) to avoid promoting
values inadvertently from parents down to their descendants.
sibling contexts in particular do not need to have the same profiles
or property sources, even though they will share common things with
their parent. Every context in the hierarchy will have its own
"bootstrap" property source (possibly empty) to avoid promoting values
inadvertently from parents down to their descendants. Every context in
the hierarchy can also (in principle) have a different
`spring.application.name` and hence a different remote property source
if there is a Config Server. Remember that properties from a child
context override those in the parent.
[[customizing-bootstrap-properties]]
=== Changing the Location of Bootstrap Properties
@@ -411,13 +414,22 @@ As an example, consider the following trivial custom locator:
public class CustomPropertySourceLocator implements PropertySourceLocator {
@Override
public PropertySource<?> locate() {
public PropertySource<?> locate(Environment environment) {
return new MapPropertySource("customProperty",
Collections.<String, Object>singletonMap("property.from.sample.custom.source", "worked as intended"));
}
}
----
The `Environment` that is passed in is the one for the
`ApplicationContext` about to be created, i.e. the one that we are
supplying additional property sources for. It will already have its
normal Spring Boot-provided property sources, so you can use those to
locate a property source specific to this `Environment` (e.g. by
keying it on the `spring.application.name`, as is done in the default
Config Server property source locator).
If you create a jar with this class in it and then add a
`META-INF/spring.factories` containing: