This article has participated in the third “topic writing” track of the Denver Creators Training Camp. For details, check out: Digg Project | Creators Training Camp third is ongoing, “write” to make a personal impact.


To allow the same Spring Boot to run in different environments, Spring allows developers to externalize configurations in a variety of ways. We can use properties files, YAML files, command-line parameters, system environment variables, and so on as configuration sources for Spring Boot applications.

Properties from the configuration can be injected into beans as follows:

  • @ValueAttributes of annotations
  • The Spring FrameworkEnvironmentabstract
  • through@ConfigurationPropertiesA structured object for binding

Spring loads configurations with a specific PropertySource priority and allows higher-priority configurations to override lower-priority configurations.

Configure the loading priority

Here are the priorities (from lowest to highest) for Spring to load configuration resources:

  1. The default Spring Boot property passesSpringApplicationIn the classsetDefaultPropertiesMethod execution.
  2. @ConfigurationOn the class@PropertySourceThe specified configuration source.
  3. Property profile. This is the most common configuration source, and different configuration files have priority relationships. (See below)
  4. attributerandom.*In theRandomValuePropertySource. Such asrandom.intI can take a random int.
  5. Environment variables of the operating system.
  6. Java System PropertiesSystem.getProperties(), it is injava -DSome properties specified in the command.
  7. java:comp/envJNDI properties in
  8. ServletContextInitialization parameter
  9. ServletConfigInitialization parameter
  10. SPRING_APPLICATION_JSONProperties in inline JSON in environment variables and system parameters are parsed and added at Spring startupEnvironmentIn the. The specific configuration modes are as follows:
    1. willSPRING_APPLICATION_JSONAs an environment variable.
    2. willspring.application.jsonAs a system attribute, such asjava -Dspring.application.json='{"name": "Tom"}' -jar app.jar.
    3. willspring.application.jsonAs a command line argument, for examplejava -jar app.jar --spring.application.json='{"name": "Tom"}'.
  11. Command-line arguments are also one of the more common sources of configuration, such as--server.port=8888, Spring adds the specified configuration toEnvironmentIf you do not want to add command line attributes, you can use theSpringApplication.setAddCommandLineProperties(false)Disable them.
  12. The test class@SpringBootTestIn the annotationspropertiesProperties.
  13. The test class@TestPropertySourceThe source of the configuration performed.
  14. The global setting property of DevTools, which takes effect when DevTools is enabled, is set in$HOME/.config/spring-bootDirectory.

Property profile

In the third point above, I mentioned properties profiles, which are the most common source of configuration for developers. Every Spring Boot application has a large number of configuration items in properties profiles. Attribute profiles are also differentiated, and they also have priority data. When different attribute values are configured for the same attribute name, the one with the highest priority overwrites the one with the lowest priority in the following order:

  • Packaged into the JARapplication.propertiesapplication.yamlFile.
  • Packaged into the JARapplication-{profile}.propertiesapplication-{profile}.yamlFile.
  • Outside of the jarapplication.propertiesapplication.yamlFile.
  • Outside of the jarapplication-{profile}.propertiesapplication-{profile}.yamlFile.

Here, it is recommended to have a consistent property profile format in a Spring Boot application. If both. Properties and. Yaml properties files exist in the same application, the. Properties file takes precedence.

Spring looks for property profiles from the following locations (priority from lowest to highest) :

  • The class path
  • Classpathconfig
  • In the current directory
  • Of the current directoryconfigdirectory
  • Of the current directoryconfigA direct subdirectory under a directory

If you don’t like the name of the application configuration file, you can specify it manually:

java -jar app.jar --spring.config.name=myconfig
Copy the code

The location of configuration files can also be specified:

java -jar app.jar --spring.config.location=\
    optional:classpath:/myconfig.properties
Copy the code

In the above directive, optional: means that the specified location does not necessarily exist in the file. If multiple paths need to be configured, use separate paths.