Monday, November 4, 2019

Spring Boot: Property File Read Order

Externalization of Config file is really important in any application, however sometime it gets confusing where we should keep the property file.

By default we tend to keep the property file in "resources folder" by naming it as "application.properties(yaml/yml)" or "application-{profile}.properties(yaml/yml)".

If you have seen any existing project where they are not using default location to store the property file, however still properties are getting loaded and if you are wondering, from where the properties are read, than here are the various locations from where Spring boot by default reads the application.properties(yaml/yml) file.

Evaluation order

1st Location: Under the classpath directly: generally build tools attach the resources folder in the classpath and while running application we can add file location in the classpath using command line.

2nd Location : under the /config folder of the classpath, in other sense just see do you have any /config folder in the classpath of the project. application.properties((yaml/yml) file inside it will be loaded.

3rd Location: under the project root directory itself.

4th Location: under the /config folder of the root directory.

The order of read is exactly as par location, the last location property will override the previous location in case of conflict.


These are the default settings, you are not happy with defaults ?

Spring Boot has hook for you in that case:

1. Do you want to change the default naming of config file ( i.e. application.properties(yaml/yml)) ? You can change it to any name, just you have to set "spring.config.name" environment variable value with the name you want to give to property file.

2.  Do you want to change the default locations ? you can change it using environment variable "spring.config.location". The only thing you have to take care of, if you are providing multiple file locations, the properties in the last file in the list will override the preceding in case of conflict. One major point to note spring will not read any default locations config files in this case.

3. Do you want to read default location files as well as the custom location file ? in that case above will not work, you have to set "spring.config.additional-location" environment variable with all additional files to read. These files will have higher precedence, in case of conflict, property values from these files will override.

4. you don't want to set environment variable ? We have to use @PropertySource annotation for loading the file. The only thing is in case of conflict, properties defined in the order above will override it. It has the second lowest priority in case of conflict.

5. Let's suppose you don't have handle of the project, you have been provided with jar only, for that kind of application we can define property files in the same location where the jar is, Spring boot will load that config and add it to jar.

Evaluation order

Properties files that are placed outside of your packaged jar override the ones inside your jar. Profile-specific files always take precedence. Properties are considered in the following order:
  • Profile-specific application properties outside of your packaged jar
  • Profile-specific application properties packaged inside your jar
  • Application properties outside of your packaged jar
  • Application properties packaged inside your jar

No comments: