How do I read system environment variables in Spring EL?

Previously you have seen that we can load properties file and read a value from it in this example: How do I read a value from properties file using Spring EL?. In this example you will learn how to read a special properties available to Spring EL. These properties include the systemEnvironment and systemProperties.

The systemEnvironment property contains all the environment variables on the machine where the program is running. Meanwhile, the systemProperties contains all the properties that we set in Java when the application started, using the -D argument. Let’s see how to access both of these properties in the following Spring configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="program1" class="org.kodejava.spring.core.el.Program">
        <property name="logPath" value="#{systemProperties['APP.LOG_PATH']}" />
    </bean>

    <bean id="program2" class="org.kodejava.spring.core.el.Program">
        <property name="logPath" value="#{systemEnvironment['TEMP']}" />
    </bean>

</beans>

In the configuration above we have two beans of Program. We set the logPath properties using a different property source. In the program1 bean we use systemProperties['APP.LOG_PATH']. Using this method the value will be pass to our program using the -DAPP.LOG_PATH=F:\Temp when we are executing the program. While the program2 bean’s logPath is read from TEMP directory property available through the systemEnvironment variables.

To make the Spring configuration works you’ll need the Program class. So here is the class definition.

package org.kodejava.spring.core.el;

public class Program {
    private String logPath;

    public Program() {
    }

    public String getLogPath() {
        return logPath;
    }

    public void setLogPath(String logPath) {
        this.logPath = logPath;
    }
}

Finally, let’s create a simple class to execute the Spring configuration file above and see the result of the code.

package org.kodejava.spring.core.el;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpELEnvironment {
    public static void main(String[] args) {
        try (ClassPathXmlApplicationContext context =
                     new ClassPathXmlApplicationContext("spel-environment.xml")) {

            Program program1 = (Program) context.getBean("program1");
            System.out.println("program.getLogPath() = " + program1.getLogPath());

            Program program2 = (Program) context.getBean("program2");
            System.out.println("program.getLogPath() = " + program2.getLogPath());
        }
    }
}

The code will print the following result:

program.getLogPath() = F:\Temp
program.getLogPath() = C:\Users\wsaryada\AppData\Local\Temp

Maven Dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.23</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.3.23</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>5.3.23</version>
    </dependency>
</dependencies>

Maven Central Maven Central Maven Central

How do I get environment variables?

Environment variables are a set of dynamic values that can affect a running process, such as our Java program. Each process usually have their own copy of these variables.

Now we would like to obtain the available variables in our environment or operating system, how do I do this in Java? Here is a code example of it.

package org.kodejava.lang;

import java.util.Map;
import java.util.Set;

public class SystemEnv {
    public static void main(String[] args) {
        // We get the environment information from the System class. 
        // The getenv method (why shouldn't it called getEnv()?) 
        // returns a map that will never have null keys or values 
        // returned.
        Map<String, String> map = System.getenv();

        Set<String> keys = map.keySet();
        for (String key : keys) {
            // Here we iterate based on the keys inside the map, and
            // with the key in hand we can get it values.
            String value = map.get(key);
            System.out.println(key + " = " + value);
        }
    }
}

Here are some results on my machine.

...
M2 = C:\ProgramData\chocolatey\lib\maven\apache-maven-3.5.0\bin
JETTY_HOME = C:\ProgramData\chocolatey\lib\jetty\tools\jetty-distribution-9.4.31.v20200723
USERNAME = wsaryada
ProgramFiles(x86) = C:\Program Files (x86)
M2_REPO = C:\Users\wsaryada\.m2
SPRING_HOME = C:\ProgramData\chocolatey\lib\spring-boot-cli\spring-2.2.4.RELEASE
M2_HOME = C:\ProgramData\chocolatey\lib\maven\apache-maven-3.5.0
JAVA_HOME = C:\Program Files\Java\jdk1.8.0_161
OS = Windows_NT
COMPUTERNAME = KRAKATAU
CATALINA_HOME = C:\tools\apache-tomcat-10.0.11
...