Home >Java >javaTutorial >How Can I Override Spring Boot\'s Default Configuration Files with External Properties?

How Can I Override Spring Boot\'s Default Configuration Files with External Properties?

Susan Sarandon
Susan SarandonOriginal
2024-11-23 22:14:12367browse

How Can I Override Spring Boot's Default Configuration Files with External Properties?

Overriding Default Configuration Files with External Configuration in Spring Boot

Spring Boot allows for the loading of multiple property files from the classpath. By default, properties from the /src/main/resources folder are loaded. However, it is also possible to specify additional external configuration files to override the default ones.

Property Configuration Order

Spring Boot loads properties in a specific order:

  1. Command line arguments
  2. System properties
  3. OS environment variables
  4. JNDI attributes
  5. Random properties
  6. External properties outside the jar
  7. Internal properties inside the jar
  8. @PropertySource annotations
  9. Default properties

Loading External Configuration Files

To load external configuration files, use the spring.config.location property, which accepts a comma-separated list of property files or file locations. For example:

-Dspring.config.location=/config/application.properties

Overriding Default Files

By default, external configuration files are added to the list of configuration sources. However, if you want to override the default files, use the spring.config.additional-location property, which only appends the specified locations. For example:

-Dspring.config.location=/config/job1.properties
-Dspring.config.additional-location=/config/job2.properties

In this case, the job1.properties file will override the default properties, while job2.properties will add additional properties.

Note on Spring Boot 2.x Behavior

In Spring Boot 2.x, the spring.config.location property now overrides the default instead of adding to it. Therefore, use spring.config.additional-location to preserve the default properties.

The above is the detailed content of How Can I Override Spring Boot's Default Configuration Files with External Properties?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn