Reputation: 43
I want to read 2 properties file from external location. It needs to be loaded via command line argument.
configuration.properties - > this is a normal property file and contains public information.
secure.properties - > this contains encrypted passwords.
I am giving following commands line parameters
-Dspring.config.location=file:C:\Project\properties\configuration.properties, file:C:\Project\properties\security\dev\secure.properties
The properties for configuration.properties
are working fine. As i am not directly loading file, but rather using the property.
Here, for this encrypted file i need to pass the path and load explicitly, which is not Happening.
Also the util EncryptedPropertiesReader
is available in jar and thus can't modify it.
Here, the catch is how i need to use the secure.properties
We have a class EncryptedPropertiesReader
.
And it contains a load(string path)
method.
Thus i need to pass the path of the secure.properties
file.
I.e.
String env = "dev"
Properties p = EncryptedPropertiesReader.load("security\" + env + "\secure.properties");
System.out.println(p); // null
Here, i can't give the absolute path because in different environments (Unix), I'll have different path.
Thus command line is my option and need to keep properties external too.
Here Are combinations tried by me :
Command line :
giving classpath for the secure. Properties
-Dspring.config.location=file:C:\Project\properties\configuration.properties, classpath:C:\Project\properties\security\dev\
giving spring.config.nane
-D spring.config.name=configuration, secure - Dspring.config.location=file:C:\Project\properties\configuration.properties, file:C:\Project\properties\security\dev\secure.properties
tried changing \ to /
path of url passed
("secure.properties");
//expected to work when i load class path
("/secure.properties");
//expected to work when i load class path
None of the above combination worked.
Any idea what's going wrong? Or what am i missing out?
Apologies for the long question.
Upvotes: 4
Views: 6866
Reputation: 10562
24.3 Application Property Files
SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).
[Note] You can also use YAML ('.yml') files as an alternative to '.properties'.
If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property. You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths).
The following example shows how to specify a different file name:
$ java -jar myproject.jar --spring.config.name=myproject
The following example shows how to specify two locations:
$ java -jar myproject.jar
--spring.config.location=classpath:/default.properties,classpath:/override.properties
[Warning] spring.config.name and spring.config.location are used very early to determine which files have to be loaded, so they must be defined as an environment property (typically an OS environment variable, a system property, or a command-line argument).
If spring.config.location contains directories (as opposed to files), they should end in / (and, at runtime, be appended with the names generated from spring.config.name before being loaded, including profile-specific file names). Files specified in spring.config.location are used as-is, with no support for profile-specific variants, and are overridden by any profile-specific properties.
Config locations are searched in reverse order. By default, the configured locations are
classpath:/,classpath:/config/,file:./,file:./config/.
The resulting search order is the following:
file:./config/ file:./ classpath:/config/ classpath:/
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:
file:./custom-config/ classpath:custom-config/
Alternatively, when custom config locations are configured by using spring.config.additional-location, they are used in addition to the default locations. Additional locations are searched before the default locations. For example, if additional locations of classpath:/custom-config/,file:./custom-config/ are configured, the search order becomes the following:
file:./custom-config/ classpath:custom-config/ file:./config/ file:./ classpath:/config/ classpath:/
This search ordering lets you specify default values in one configuration file and then selectively override those values in another. You can provide default values for your application in application.properties (or whatever other basename you choose with spring.config.name) in one of the default locations. These default values can then be overridden at runtime with a different file located in one of the custom locations.
[Note] If you use environment variables rather than system properties, most operating systems disallow period-separated key names, but you can use underscores instead (for example, SPRING_CONFIG_NAME instead of spring.config.name).
[Note] If your application runs in a container, then JNDI properties (in java:comp/env) or servlet context initialization parameters can be used instead of, or as well as, environment variables or system properties.
Upvotes: 2
Reputation: 11055
This is how u can load properties from any location using environment variable
-Dspring.config.location="C:\Project\properties\", -Dsecure.properties.location="C:\Project\properties\security\dev\"
@PropertySources({
@PropertySource("file:${spring.config.location}/configuration.properties"),
@PropertySource("file:${secure.properties.location}/secure.properties")})
Upvotes: 3