Environment variables in Java and Spring boot

Environment variables in Java and Spring boot

Environment variables are OS-specific. Those are stored for global usage. For example, we install Java in a specific path. The common path is "C:\Program Files\Java\jdk-17.0.5\bin" in Windows. If we want to do something in the command line using Java, each time we need to write this path. Instead, we can store this path inside a variable and use it. This is a picture from a developer's perspective. But also environment variables are used in production servers. Another common usage is keeping secrets and credentials in production servers.

Some real-world usage of environment variables

  • Store database URL and credentials

  • Configurations of software like Apache, logging path etc.

  • Any kind of sensitive information and so on.

The advantage is that those things are changed frequently. We don't need to redeploy the application, just change the variable value.

Environment Variables in Java

Environment variables are key-value pairs. Java provides a static method called System.getEnv() which returns an unmodifiable map. This map contains the environment variables and their corresponding values as a key-value pair. If we don't pass an argument in System.getenv() then it will retrieve all the environment variables from the system.

public class Main {
    public static void main(String[] args) {
        Map<String, String> list = System.getenv();

        list.forEach((env, value) -> System.out.println(env+"="+System.getenv(env)));
    }
}

Now, if we want a specific variable, we need to pass the key as an argument.

System.out.println(System.getenv("JAVA_HOME"));

Environment variables in spring boot

Spring boot lets us externalize our configuration. We can use the properties file, yaml file, environment variables and command line arguments to externalize our configurations. We can directly inject those property values into our beans by using the @Value annotation.

@Value("${JAVA_HOME}")
private String envDirect;

Using properties files

We can also use our environment variables in properties files too. Today I'm using the spring-boot provided application.properties file.

java.home=${JAVA_HOME}

Now, Inject it into a bean as usual.

@Value("${java.home}")
private String envViaProperties;

I've tested those values like this,

@Test
public void directEnvTest() {
    Assertions.assertEquals("C:\\Program Files\\Java\\jdk-17.0.5", bean.getEnvDirect());
}

@Test
public void viaPropertiesEnvTest() {
    Assertions.assertEquals("C:\\Program Files\\Java\\jdk-17.0.5", bean.getEnvViaProperties());
}

Environment class

Spring has a class that also gives us the environment variables. This class is a part of Spring Core. The package is 'org.springframework.core.env.Environment'.

@SpringBootTest
public class DefaultEnvClassTest {
    @Autowired
    private Environment environment;

    @Test
    public void test() {
        Assertions.assertEquals("C:\\Program Files\\Java\\jdk-17.0.5", environment.getProperty("JAVA_HOME"));
    }
}

Also, we can use the System.getenv() method from standard java.lang package.

@Test
public void testWithJavaLangMethod() {
    Assertions.assertEquals("C:\\Program Files\\Java\\jdk-17.0.5", System.getenv("JAVA_HOME"));
}

The source code is here.

Thank you so much for reading this article. Ask me in the comment, if you have any questions