The difference between application.yml and bootstrap.yml

The difference between application.yml and bootstrap.yml


Spring Boot **supports properties (.properties) and YAML (.yml .yaml )** by default. Both formats of configuration files, yml and properties files, are configuration files and have the same functions.
Spring Cloud is built on Spring Boot. There are two contexts in Spring Boot, one is **bootstrap** and the other is **application**. The differences between these two configuration files are listed below.

Loading order

If application.yml and bootstrap.yml are in the same directory: **bootstrap.yml is loaded first and then **application.yml is loaded.

bootstrap.yml is used for the bootstrap phase of the application context. bootstrap.yml is loaded by the parent Spring ApplicationContext.

Configuration differences

Both bootstrap.yml and application.yml can be used to configure parameters.

bootstrap.yml is used to execute when the program boots and is used to read configuration information earlier . It can be understood as some parameter configurations at the system level. These parameters generally do not change. Once bootStrap.yml is loaded, the contents are not overwritten.

application.yml can be used to define application-level , application-specific configuration information, and can be used to configure public parameters to be used in subsequent modules , etc.

Attribute coverage problem

When starting a context, Spring Cloud creates a Bootstrap Context as the parent context of the Spring application's Application Context.

During initialization, the Bootstrap Context is responsible for loading configuration properties from external sources and parsing configurations. The two contexts share an Environment obtained from the outside. Bootstrap properties have high priority and by default they are not overridden by local configuration.

That is to say, if the content tag of the loaded application.yml is consistent with the bootstrap tag, application will not overwrite bootstrap, and the content in application.yml can be dynamically replaced.

Typical application scenarios of bootstrap.yml

When using the Spring Cloud Config Server configuration center, you need to specify spring.application.name and spring.cloud.config.server.git.uri in the bootstrap.yml configuration file, and add configuration properties connected to the configuration center to load external The configuration information of the configuration center
has some fixed attributes that cannot be overwritten and
some encryption/decryption scenarios.

Summarize:

bootstrap.yml is used to execute when the program boots and is used to read configuration information earlier.
application.yml can be used to define application-level , application-specific configuration information, and can be used to configure public parameters to be used in subsequent modules , etc.

Guess you like

Origin blog.csdn.net/weixin_44030143/article/details/130223715