SoFunction
Updated on 2025-03-01

SpringBoot configuration file priority order, load order, difference and description

Spring Boot Configuration File

Priority order

In Spring Boot, the priority order of configuration files is:

application-{profile}.ymlapplication-{profile}.properties) > ) > )。

in,{profile}Represents different environment configurations, such asdevtestprodwait.

Priority is from high to low. High priority configurations cover low priority configurations, and all configurations will form complementary configurations.

Loading order

In Spring Boot, the loading order of configuration files is as follows:

  1. or: This is the first configuration file to load, used to configure the infrastructure of the application context, such as external configuration sources and encryption/decryption.
  2. or: This is the main configuration file that contains the general configuration of the application.
  3. application-{profile}.yml or application-{profile}.properties: Load the corresponding configuration file for different environments (profiles). For example,Used in the development environment,Used in production environment.

When loading a configuration file, Spring Boot will search and load the corresponding configuration items in the above order.

When a configuration item with the same name exists,

The configuration file loaded later overwrites the configuration item with the same key name loaded previously. This order ensures that higher priority configuration files can cover lower priority configurations, achieving flexible configuration management.

What is the difference and?

andThey are all commonly used configuration files in Spring Boot projects.

The main differences between them are as follows:

1. Different uses:

  • Files are mainly used to configure the startup parameters of Spring Boot applications, such as database connections, server ports, etc. These parameters need to be set when the application starts, so put them inin the file.
  • Files are used to configure global parameters of Spring Boot applications, such as log levels, cache policies, etc. These parameters can be changed while the application is running, but once changed, all places where these parameters are used will be affected, so put them inin the file.

2. Different contents:

  • Files usually contain some hard-coded configuration values, such as database connection URLs, server ports, etc. These values ​​need to be set when the application starts, so put them inin the file.
  • Files usually contain some configurable values, such as log level, cache policy, etc. These values ​​can be changed when the application runs, but once changed, all places where these values ​​are used will be affected, so put them inin the file.

3. Different order:

  • The file is inThe file is loaded before, so inConfiguration values ​​defined in the fileWill cover The same configuration value in the file.

Anyway,Files are mainly used to configure the startup parameters of the application, andFiles are used to configure global parameters of the application.

In actual projects, you can choose which file to use or two files at the same time according to your needs.

Spring Boot projects may not be used directly, but they are used in Spring Cloud projects to define some additional configurations related to Spring Cloud components, such as service discovery and configuration servers.

Practical application scenarios

In actual projects, usuallyThere are someRecommended best practices, such as:

  • The necessary configuration items for startup such as database connection information may be placed in
  • Application-level configuration items, log levels, cache policies, etc.

butSome projects may not be used, instead, it uses the Nacos configuration center or command line running parameters for configuration.

This flexibility allows the development team to choose the most suitable configuration method for the project according to actual needs, so as to better adapt to different environments and application scenarios.

Summarize

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