Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regarding application.properties file and environment variable

Java successfully recognizes the path in my application.properties file when I have the path configured as below:

pathToInputFile=/kcs/data/incoming/ready/ pathToInputFileProcess=/kcs/data/incoming/work/ 

If I try using an environment variable, the Java program doesn't recognize the path.

(the environmental variable TOM_DATA is set as /kcs.)

pathToInputFile=${TOM_DATA}/data/incoming/ready/ pathToInputFileProcess=${TOM_DATA}/data/incoming/work/ 

Can I use an environment variable inside application.properties file?

like image 826
Arav Avatar asked Feb 15 '10 03:02

Arav


People also ask

Can we use environment variables in properties file?

You can put environment variables in your properties file, but Java will not automatically recognise them as environment variables and therefore will not resolve them. In order to do this you will have to parse the values and resolve any environment variables you find.

What is properties file in application?

Properties files are used to keep 'N' number of properties in a single file to run the application in a different environment. In Spring Boot, properties are kept in the application. properties file under the classpath. The application.properties file is located in the src/main/resources directory.

Does environment variable override application properties?

Now, when your Spring Boot application starts, it will be given those environment variables, which will override your application. properties .


2 Answers

You can put environment variables in your properties file, but Java will not automatically recognise them as environment variables and therefore will not resolve them.

In order to do this you will have to parse the values and resolve any environment variables you find.

You can get at environment variables from Java using various methods. For example: Map<String, String> env = System.getenv();

There's a basic tutorial here: http://java.sun.com/docs/books/tutorial/essential/environment/env.html

Hope that's of some help.

like image 122
Tom Duckering Avatar answered Oct 07 '22 01:10

Tom Duckering


Tom Duckering's answer is correct. Java doesn't handle this for you.

Here's some code utilizing regular expressions that I wrote to handle environment variable substitution:

/*  * Returns input string with environment variable references expanded, e.g. $SOME_VAR or ${SOME_VAR}  */ private String resolveEnvVars(String input) {     if (null == input)     {         return null;     }     // match ${ENV_VAR_NAME} or $ENV_VAR_NAME     Pattern p = Pattern.compile("\\$\\{(\\w+)\\}|\\$(\\w+)");     Matcher m = p.matcher(input); // get a matcher object     StringBuffer sb = new StringBuffer();     while(m.find()){         String envVarName = null == m.group(1) ? m.group(2) : m.group(1);         String envVarValue = System.getenv(envVarName);         m.appendReplacement(sb, null == envVarValue ? "" : envVarValue);     }     m.appendTail(sb);     return sb.toString(); } 
like image 22
Tim Lewis Avatar answered Oct 07 '22 00:10

Tim Lewis