0

I am trying to read some credentials from env variables in custom.properties file but it's not able to recognize code for my properties file given below but it's not working the way I am fetching env variable like this "${varName}"

org.apache.ws.security.crypto.provider=org.apache.wss4j.common.crypto.Merlin
# Type - Valid Keystore Type. Eg - pkcs12 , jks   
org.apache.ws.security.crypto.merlin.keystore.type=jks
# Keystore Password 
org.apache.ws.security.crypto.merlin.keystore.password=${keystorePassword}
# Keystore Private Password
org.apache.ws.security.crypto.merlin.keystore.private.password=${keystorePassword}
# Keystore Alias
org.apache.ws.security.crypto.merlin.keystore.alias=${keystoreAlias}
# Keystore File Name
org.apache.ws.security.crypto.merlin.keystore.file="something.jks"

can someone please help me out how to get env variables to custom.properties file and

  • Add properties to Spring application.properties file. Else you need to write a Utility class to read property value with environment variables. – RLD Sep 18 '20 at 13:45
  • Check this answer https://stackoverflow.com/a/25884743/8805742 – mahfuj asif Sep 18 '20 at 15:23
  • no what I want is to get the values from environment variables to custom property file don't want to get the value from custom.property but I. want the value in custom property from env variable – Divyank Vijayvergiya Sep 18 '20 at 16:23

2 Answers2

0

Kindly try with the below sample code .

it reads properties from custom properties file and with prefix value . for eg :Below example reads value from properties with the key of org.apache.ws.security.crypto.provider

@Configuration
@PropertySource(value = "classpath:<properytfilename>.properties")
@ConfigurationProperties(prefix = "org.apache.ws.security.crypto")
public class CustomProperties {

    private String provider;

}
sathiya raj
  • 35
  • 1
  • 5
  • this code is to get the values from custom property what I want is to get the values from environment variables to custom property file don't want to get the value from custom.property but I. want the value in custom property from env variable – Divyank Vijayvergiya Sep 18 '20 at 16:23
0

Use java.util.Properties


....
@Value("${what.ever.env}")
private String keystorePassword;

...
            Properties properties = new Properties();
            properties.setProperty("org.apache.ws.security.crypto.provider", "org.apache.wss4j.common.crypto.Merlin");
            properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", keystorePassword);


            File file = new File("custom.properties");
            FileOutputStream fileOut = new FileOutputStream(file);
            properties.store(fileOut, "Comment here");
            fileOut.close();
...
Chayne P. S.
  • 1,558
  • 12
  • 17
  • but this is to write the files at runtime what I am trying I want to read directly to the properties file from env variables like we can read in application.properties file – Divyank Vijayvergiya Sep 21 '20 at 10:08