How to understand the loading order of Spring Boot configurations?

The loading order of Spring Boot configuration mainly consists of the following steps:

  1. Command line arguments: Configurations specified through command line arguments will take precedence over other configurations. For example, you can specify the port number of the application by using –server.port=8080.
  2. The application.properties or application.yml file from outside the jar package: You can define configurations in a file outside the jar package, and Spring Boot will automatically load them. For example, you can place a custom application.properties file at the same level as the jar package and define configurations in it.
  3. Spring Boot automatically loads the application.properties or application.yml file from within the JAR file. The configurations in these files will override the default configurations in the JAR file.
  4. Additional configuration files loaded via the @PropertySource annotation can be used on the application’s configuration class. The configurations in these files will override the default configurations.
  5. Configurations loaded via @ConfigurationProperties annotation: You can use the @ConfigurationProperties annotation in your application’s configuration class to load configurations. These configurations will override the default configurations.
  6. Configuration loaded via @Value annotation: Using the @Value annotation in the application’s components allows for loading configuration settings that will override the default configurations.

It is important to note that the later a configuration is loaded, the higher its priority, and it will override any previously loaded configurations.

bannerAds