A Spring Cloud application operates by creating a "bootstrap" context, which is a parent context for the main application. Out of the box it is
responsible for loading configuration properties from the external sources, and also decrypting properties in the local external configuration
files. The two contexts share an Environment which is the source of external properties for any Spring application. Bootstrap properties are
added with high precedence, so they cannot be overridden by local configuration, by default.
The bootstrap context uses a different convention for locating external configuration than the main application context, so instead of
application.yml (or .properties ) you use bootstrap.yml , keeping the external configuration for bootstrap and main context nicely
separate. Example:
bootstrap.yml.
spring:
application:
name: foo
cloud:
config:
uri: ${SPRING_CONFIG_URI:http://localhost:8888}
It is a good idea to set the spring.application.name (in bootstrap.yml or application.yml ) if your application needs any application-
specific configuration from the server.
You can disable the bootstrap process completely by setting spring.cloud.bootstrap.enabled=false (e.g. in System properties).
2.2 Application Context Hierarchies
If you build an application context from SpringApplication or SpringApplicationBuilder , then the Bootstrap context is added as a
parent to that context. It is a feature of Spring that child contexts inherit property sources and profiles from their parent, so the "main"
application context will contain additional property sources, compared to building the same context without Spring Cloud Config. The additional
property sources are:
"bootstrap": an optional CompositePropertySource appears with high priority if any PropertySourceLocators are found in the
Bootstrap context, and they have non-empty properties. An example would be properties from the Spring Cloud Config Server. See below
for instructions on how to customize the contents of this property source.
"applicationConfig: [classpath:bootstrap.yml]" (and friends if Spring profiles are active). If you have a bootstrap.yml (or properties) then
those properties are used to configure the Bootstrap context, and then they get added to the child context when its parent is set. They have
lower precedence than the application.yml (or properties) and any other property sources that are added to the child as a normal part
of the process of creating a Spring Boot application. See below for instructions on how to customize the contents of these property
sources.
Because of the ordering rules of property sources the "bootstrap" entries take precedence, but note that these do not contain any data from
bootstrap.yml , which has very low precedence, but can be used to set defaults.
You can extend the context hierarchy by simply setting the parent context of any ApplicationContext you create, e.g. using its own
interface, or with the SpringApplicationBuilder convenience methods ( parent() , child() and sibling() ). The bootstrap context will
be the parent of the most senior ancestor that you create yourself. 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. Normal Spring
application context behaviour rules apply to property resolution: properties from a child context override those in the parent, by name and also
by property source name (if the child has a property source with the same name as the parent, the one from the parent is not included in the
child).
Note that the SpringApplicationBuilder allows you to share an Environment amongst the whole hierarchy, but that is not the default.
Thus, 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.
2.3 Changing the Location of Bootstrap Properties
The bootstrap.yml (or .properties ) location can be specified using spring.cloud.bootstrap.name (default "bootstrap") or
spring.cloud.bootstrap.location (default empty), e.g. in System properties. Those properties behave like the spring.config.*
variants with the same name, in fact they are used to set up the bootstrap ApplicationContext by setting those properties in its
Environment . If there is an active profile (from spring.profiles.active or through the Environment API in the context you are building)
then properties in that profile will be loaded as well, just like in a regular Spring Boot app, e.g. from bootstrap-development.properties for
a "development" profile.