Multiple external property file in spring boot from command line

Education Master :

I want to read 2 properties file from external location. It needs to be loaded via command line argument.

  1. configuration.properties - > this is a normal property file and contains public information.

  2. 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 :

    1. giving classpath for the secure. Properties

      -Dspring.config.location=file:C:\Project\properties\configuration.properties, classpath:C:\Project\properties\security\dev\
      
    2. 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
      
    3. tried changing \ to /


  • path of url passed

    1. ("secure.properties"); //expected to work when i load class path

    2. ("/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.

Niraj Sonawane :

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")})

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=81510&siteId=1