SoFunction
Updated on 2025-03-08

The difference and description of SpringBoot configuration file bootstrap and application

The difference between SpringBoot configuration file bootstrap and application

There are two types of configuration supported by springboot: four bootsrap application

Yml file:

  • ()
  • ()

properties properties property file:

  •  
  •  

The difference between bootstrap and application

1. Differences in loading order:

The bootstrap configuration file is loaded first over the application configuration file. bootstrap is loaded by the Spring parent context (parent ApplicationContext), and the application is loaded by the Spring child context.

2. Differences in priority:

The properties in bootstrap cannot be overwritten. If the two types of files, bootstrap and application, are mainly bootstrap configuration files.

3. Usage scenarios:

bootstrap is mainly used to configure some parameters at the system level.

  • Some fixed parameters that cannot be overwritten
  • When you use Spring Cloud Config Configuration Center, you need to add configuration properties connected to the Configuration Center in the boostrap configuration file to load the configuration information of the external configuration center.

Application is used to configure some application-level parameters

  • Configuration of third-party data sources
  • Rabbitmq,redis configuration

Comparative analysis of bootstrap and application

Anyone who has used Spring Boot knows that there are the following two configuration files in Spring Boot

  • bootstrap (.yml or .properties)
  • application (.yml or .properties)

Why are there these two configuration files? Do you all know their differences and specific usage scenarios?

The difference between bootstrap/application

I went to the official Spring Boot document and found no specific definitions about these two files. Then I searched the official Spring Cloud document to find the difference.

/spring-cloud-static/Greenwich.SR1/single/#_the_bootstrap_application_context

I carefully read the following document, and the original text roughly means this.

Spring Cloud is built on Spring Boot. There are two contexts in Spring Boot. One is bootstrap and the other is application. bootstrap is the parent context of the application, which means bootstrap loading takes precedence over application.

bootstrap is mainly used to load configuration information from additional resources, and can also decrypt properties in local external configuration files.

These two contexts share an environment, which is the source of external properties of any Spring application.

Properties in bootstrap will be loaded first, and they cannot be overwritten by the same local configuration by default.

Therefore, compared with the application configuration file, the bootstrap configuration file has the following characteristics.

  • bootstrap is loaded by the parent ApplicationContext and is loaded first than application
  • Properties in boostrap cannot be overwritten

Application scenarios of bootstrap/application

The application configuration file is easy to understand and is mainly used for the automated configuration of Spring Boot projects.

The bootstrap configuration file has the following application scenarios.

  • 1. When using Spring Cloud Config to configure the center, you need to add configuration properties connected to the configuration center in the bootstrap configuration file to load the configuration information of the external configuration center;
  • 2. Some fixed attributes that cannot be overwritten
  • 3. Some encryption/decryption scenarios;

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.