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"
        <property name="logPath"
    <bean id="program2"
        <property name="logPath"

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/wsaryada/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.model;

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;

import org.kodejava.example.spring.model.MyDataSource;
import org.kodejava.example.spring.model.Program;
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:

INFO: Loading XML bean definitions from class path resource [spel-environment.xml]
program.getLogPath() = /Users/wsaryada/tmp
program.getLogPath() = /Users/wsaryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.

Leave a Reply