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 includes 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=""

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

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


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=/Users/wayan/tmp when we are executing the program. While the program2 bean’s logPath is read from user’s home 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.example.spring.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.example.spring.el;

import org.springframework.context.ApplicationContext;

public class SpELEnvironment {
    public static void main(String[] args) {
        ApplicationContext 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() = /Users/wayan/tmp
program.getLogPath() = /Users/wayan

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.example.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 result on my machine.

PATH = /opt/local/bin:/opt/local/sbin:~bin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/gradle/gradle-2.10/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
SHELL = /bin/bash
JAVA_HOME = /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home
USER = wsaryada
GRADLE_HOME = /usr/local/gradle/gradle-2.10
HOME = /Users/wsaryada