Spring Boot Environment

Spring Boot Environment shows how to read environment variables in Spring Boot. A Spring Boot application can be deployed in a variety of environments and reading environment variables can be helpful in such cases.

Spring is a popular Java application framework and Spring Boot is a next step of evolution of Spring which helps create stand-alone, production-grade Spring based applications with minimal effort.

Environment is an interface representing the environment in which the current application is running. It can be use to get profiles and properties of the application environment.

$ echo $JAVA_HOME

In this sample case, we have a JAVA_HOME environment variable defined.

$ tree
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── zetcode
    │   │           └── Application.java
    │   └── resources
    │       ├── application.properties
    │       └── logback.xml 
    └── test
        └── java

This is the project structure of the Spring Boot application.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 




This is the Maven build file. The spring-boot-starter is the core starter that includes auto-configuration support, logging, and YAML. The application is packaged into a JAR file.


The application.properties file contains application configuration settings. Spring has some built-in application properties and we can create our custom ones. The spring.main.banner-mode property is a Spring built-in property; we turn off the Spring's banner. The app.name is our custom property that contains the application name.

<?xml version="1.0" encoding="UTF-8"?>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="org.springframework" level="ERROR"/>
    <logger name="com.zetcode" level="ERROR"/>

The application logging is configured in the logback.xml file. We set the level of logging to ERROR. This way our output is not cluttered with unnecessary messages. The spring-boot-starter dependency enables logback for logging.

package com.zetcode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;

public class Application implements CommandLineRunner {

    private Environment env;    
    public void run(String... args) throws Exception {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

In the Application, we create a bean, call its method and set up the Spring Boot application. The CommandLineRunner interface indicates that a bean should run when it is contained within a SpringApplication. It can be used to create command line applications in Spring Boot.

public class Application implements CommandLineRunner {

The @SpringBootApplication annotation enables auto-configuration and component scanning.

private Environment env;  

We inject the Environment in order to obtain the properties.


Here, we retrieve the JAVA_HOME environment variable.


Environment can be used to get the properties from the application.properties file as well: get the app.name property.

$ mvn spring-boot:run -q

We run the application. The -q Maven option turns off Maven messages.

In this tutorial, we have used Spring Environment to read environment variable. You might also be interested in the related tutorials: SpringApplicationBuilder tutorial, Spring Boot Thymeleaf tutorial, Spring Boot @Qualifier annotation, Serving static content in Spring Boot, Spring Boot DataSourceBuilder tutorial, Spring Boot iText tutorial, Spring Boot RESTFul application, Java tutorial.