08-02-2023, 12:30 PM
Spring boot 1.X and Spring Boot 2.X don't provide the same options and behavior about the [`Externalized Configuration`][1].
The very good answer of M. Deinum refers to Spring Boot 1 specifities.
**I will update for Spring Boot 2 here.**
**Environment properties sources and order**
Spring Boot 2 uses a very particular `PropertySource` order that is designed to allow sensible overriding of values. Properties are considered in the following order:
> - Devtools global settings properties on your home directory
> (~/.spring-boot-devtools.properties when devtools is active).
>
> - `@TestPropertySource` annotations on your tests.
>
> - `@SpringBootTest#properties` annotation attribute on your tests. Command
> line arguments.
>
> - Properties from `SPRING_APPLICATION_JSON` (inline JSON embedded in an
> environment variable or system property).
>
> - `ServletConfig` init parameters.
>
> - `ServletContext` init parameters.
>
> - JNDI attributes from `java:comp/env`.
>
> - Java System properties (`System.getProperties()`).
>
> - OS environment variables.
>
> - A `RandomValuePropertySource` that has properties only in random.*.
>
> - Profile-specific application properties outside of your packaged jar
> (`application-{profile}.properties` and YAML variants).
>
> - Profile-specific application properties packaged inside your jar
> (`application-{profile}.properties` and YAML variants).
>
> - Application properties outside of your packaged jar
> (`application.properties` and YAML variants).
>
> - Application properties packaged inside your jar
> (`application.properties` and YAML variants).
>
> - `@PropertySource` annotations on your `@Configuration` classes. Default
> properties (specified by setting
> `SpringApplication.setDefaultProperties`).
To specify external properties files these options should interest you :
> - Profile-specific application properties outside of your packaged jar
> (`application-{profile}.properties` and YAML variants).
>
> - Application properties outside of your packaged jar
> (`application.properties` and YAML variants).
>
>
> - `@PropertySource` annotations on your `@Configuration` classes. Default
> properties (specified by setting
> `SpringApplication.setDefaultProperties`).
You can use only one of these 3 options or combine them according to your requirements.
For example for very simple cases using only profile-specific properties is enough but in other cases you may want to use both profile-specific properties, default properties and `@PropertySource`.
**Default locations for application.properties files**
About `application.properties` files (and variant), by default Spring loads them and add their properties in the environment from these in the following order :
> - A /config subdirectory of the current directory
>
> - The current directory
>
> - A classpath /config package
>
> - The classpath root
The higher priorities are so literally :
`classpath:/,classpath:/config/,file:./,file:./config/`.
**How to use properties files with specific names ?**
The default locations are not always enough : the default locations like the default filename (`application.properties`) may not suit. Besides, as in the OP question you may need to specify multiple configuration files other than `application.properties` (and variant).
So `spring.config.name` will not be enough.
In this case you should provide an explicit location by using the `spring.config.location` environment property (which is a comma-separated list of directory locations or file paths).
To be free about the filenames pattern favor the list of file paths over the list of directories.
For example do like that :
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
That way is the most verbose that just specifying the folder but it is also the way to specify very finely our configuration files and to document clearly the properties effectively used.
**spring.config.location now replaces default locations instead of adding to them**
With Spring Boot 1, the `spring.config.location` argument adds specified locations in the Spring environment.
But from Spring Boot 2, `spring.config.location` replaces the default locations used by Spring by the specified locations in the Spring environment as stated [in the documentation][2].
> When custom config locations are configured by using
> `spring.config.location`, they replace the default locations. For
> example, if `spring.config.location` is configured with the value
> `classpath:/custom-config/`,`file:./custom-config/`, the search order
> becomes the following:
>
> 1. `file:./custom-config/`
> 2. `classpath:custom-config/`
`spring.config.location` is now a way to make sure that any `application.properties` file has to be explicitly specified.
For uber JARs that are not supposed to package `application.properties` files, that is rather nice.
To keep the old behavior of `spring.config.location` while using Spring Boot 2 you can use the new `spring.config.additional-location` property instead of `spring.config.location` that still adds the locations [as stated by the documentation][2] :
> Alternatively, when custom config locations are configured by using
> `spring.config.additional-location`, they are used in addition to the
> default locations.
----------
**In practice**
So supposing that as in the OP question, you have 2 external properties file to specify and 1 properties file included in the uber jar.
To use only configuration files you specified :
-Dspring.config.location=classpath:/job1.properties,classpath:/job2.properties,classpath:/applications.properties
To add configuration files to these in the default locations :
-Dspring.config.additional-location=classpath:/job1.properties,classpath:/job2.properties
`classpath:/applications.properties` is in the last example not required as the default locations have that and that default locations are here not overwritten but extended.
[1]:
The very good answer of M. Deinum refers to Spring Boot 1 specifities.
**I will update for Spring Boot 2 here.**
**Environment properties sources and order**
Spring Boot 2 uses a very particular `PropertySource` order that is designed to allow sensible overriding of values. Properties are considered in the following order:
> - Devtools global settings properties on your home directory
> (~/.spring-boot-devtools.properties when devtools is active).
>
> - `@TestPropertySource` annotations on your tests.
>
> - `@SpringBootTest#properties` annotation attribute on your tests. Command
> line arguments.
>
> - Properties from `SPRING_APPLICATION_JSON` (inline JSON embedded in an
> environment variable or system property).
>
> - `ServletConfig` init parameters.
>
> - `ServletContext` init parameters.
>
> - JNDI attributes from `java:comp/env`.
>
> - Java System properties (`System.getProperties()`).
>
> - OS environment variables.
>
> - A `RandomValuePropertySource` that has properties only in random.*.
>
> - Profile-specific application properties outside of your packaged jar
> (`application-{profile}.properties` and YAML variants).
>
> - Profile-specific application properties packaged inside your jar
> (`application-{profile}.properties` and YAML variants).
>
> - Application properties outside of your packaged jar
> (`application.properties` and YAML variants).
>
> - Application properties packaged inside your jar
> (`application.properties` and YAML variants).
>
> - `@PropertySource` annotations on your `@Configuration` classes. Default
> properties (specified by setting
> `SpringApplication.setDefaultProperties`).
To specify external properties files these options should interest you :
> - Profile-specific application properties outside of your packaged jar
> (`application-{profile}.properties` and YAML variants).
>
> - Application properties outside of your packaged jar
> (`application.properties` and YAML variants).
>
>
> - `@PropertySource` annotations on your `@Configuration` classes. Default
> properties (specified by setting
> `SpringApplication.setDefaultProperties`).
You can use only one of these 3 options or combine them according to your requirements.
For example for very simple cases using only profile-specific properties is enough but in other cases you may want to use both profile-specific properties, default properties and `@PropertySource`.
**Default locations for application.properties files**
About `application.properties` files (and variant), by default Spring loads them and add their properties in the environment from these in the following order :
> - A /config subdirectory of the current directory
>
> - The current directory
>
> - A classpath /config package
>
> - The classpath root
The higher priorities are so literally :
`classpath:/,classpath:/config/,file:./,file:./config/`.
**How to use properties files with specific names ?**
The default locations are not always enough : the default locations like the default filename (`application.properties`) may not suit. Besides, as in the OP question you may need to specify multiple configuration files other than `application.properties` (and variant).
So `spring.config.name` will not be enough.
In this case you should provide an explicit location by using the `spring.config.location` environment property (which is a comma-separated list of directory locations or file paths).
To be free about the filenames pattern favor the list of file paths over the list of directories.
For example do like that :
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
That way is the most verbose that just specifying the folder but it is also the way to specify very finely our configuration files and to document clearly the properties effectively used.
**spring.config.location now replaces default locations instead of adding to them**
With Spring Boot 1, the `spring.config.location` argument adds specified locations in the Spring environment.
But from Spring Boot 2, `spring.config.location` replaces the default locations used by Spring by the specified locations in the Spring environment as stated [in the documentation][2].
> When custom config locations are configured by using
> `spring.config.location`, they replace the default locations. For
> example, if `spring.config.location` is configured with the value
> `classpath:/custom-config/`,`file:./custom-config/`, the search order
> becomes the following:
>
> 1. `file:./custom-config/`
> 2. `classpath:custom-config/`
`spring.config.location` is now a way to make sure that any `application.properties` file has to be explicitly specified.
For uber JARs that are not supposed to package `application.properties` files, that is rather nice.
To keep the old behavior of `spring.config.location` while using Spring Boot 2 you can use the new `spring.config.additional-location` property instead of `spring.config.location` that still adds the locations [as stated by the documentation][2] :
> Alternatively, when custom config locations are configured by using
> `spring.config.additional-location`, they are used in addition to the
> default locations.
----------
**In practice**
So supposing that as in the OP question, you have 2 external properties file to specify and 1 properties file included in the uber jar.
To use only configuration files you specified :
-Dspring.config.location=classpath:/job1.properties,classpath:/job2.properties,classpath:/applications.properties
To add configuration files to these in the default locations :
-Dspring.config.additional-location=classpath:/job1.properties,classpath:/job2.properties
`classpath:/applications.properties` is in the last example not required as the default locations have that and that default locations are here not overwritten but extended.
[1]:
[To see links please register here]
[2]:[To see links please register here]