Customize the set of
PropertySource objects to be searched by this
Environment during calls to
#getProperty(String) and related
methods.
Subclasses that override this method are encouraged to add property
sources using
MutablePropertySources#addLast(PropertySource) such that
further subclasses may call
super.customizePropertySources() with
predictable results. For example:
public class Level1Environment extends AbstractEnvironment {
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
super.customizePropertySources(propertySources); // no-op from base class
propertySources.addLast(new PropertySourceA(...));
propertySources.addLast(new PropertySourceB(...));
}
}
public class Level2Environment extends Level1Environment {
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
super.customizePropertySources(propertySources); // add all from superclass
propertySources.addLast(new PropertySourceC(...));
propertySources.addLast(new PropertySourceD(...));
}
}
In this arrangement, properties will be resolved against sources A, B, C, D in that
order. That is to say that property source "A" has precedence over property source
"D". If the
Level2Environment subclass wished to give property sources C
and D higher precedence than A and B, it could simply call
super.customizePropertySources after, rather than before adding its own:
public class Level2Environment extends Level1Environment {
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(new PropertySourceC(...));
propertySources.addLast(new PropertySourceD(...));
super.customizePropertySources(propertySources); // add all from superclass
}
}
The search order is now C, D, A, B as desired.
Beyond these recommendations, subclasses may use any of the
add*,
remove, or
replace methods exposed by
MutablePropertySourcesin order to create the exact arrangement of property sources desired.
The base implementation registers no property sources.
Note that clients of any
ConfigurableEnvironment may further customize
property sources via the
#getPropertySources() accessor, typically within
an
org.springframework.context.ApplicationContextInitializer. For example:
ConfigurableEnvironment env = new StandardEnvironment();
env.getPropertySources().addLast(new PropertySourceX(...));
A warning about instance variable access
Instance variables declared in subclasses and having default initial values should
not be accessed from within this method. Due to Java object creation
lifecycle constraints, any initial value will not yet be assigned when this
callback is invoked by the
#AbstractEnvironment() constructor, which may
lead to a
NullPointerException or other problems. If you need to access
default values of instance variables, leave this method as a no-op and perform
property source manipulation and instance variable access directly within the
subclass constructor. Note that
assigning values to instance variables is
not problematic; it is only attempting to read default values that must be avoided.