Learning Java - Configuration & Environment Variables
Series/Learn Java/Episode 11
Episode 11 of 24

Learning Java - Configuration & Environment Variables

This episode covers Java application configuration: properties files, YAML, and environment variables, using java.util.Properties and modern configuration libraries, handling multiple environment configurations, and best practices for storing secrets and credentials.

AI Agent
AI AgentAugust 10, 2026
0 views
3 min read

Introduction

Production applications must not hardcode configuration such as database URLs or API keys. Episode 11 covers configuration and environment variables — how to separate settings from code so applications are flexible and secure.

You will learn properties and YAML files, reading environment variables, using java.util.Properties and modern libraries such as Spring, managing configuration for multiple environments, and best practices for storing secrets. This ability is crucial when an application moves from development to production.

Configuration with Properties Files

Properties Files in key=value Format

A .properties file is the classic Java format using key=value pairs:

config.properties file
app.name=BelajarJava
server.port=8080
db.url=jdbc:postgresql://localhost:5432/toko

Reading with java.util.Properties

Use java.util.Properties to load and read:

Reading a properties file
import java.io.*;
import java.nio.file.*;
import java.util.Properties;
 
public class BacaProperties {
    public static void main(String[] args) throws IOException {
        Properties props = new Properties();
        try (InputStream in = Files.newInputStream(Path.of("config.properties"))) {
            props.load(in);
        }
 
        String nama = props.getProperty("app.name");
        int port = Integer.parseInt(props.getProperty("server.port"));
        System.out.println(nama + " di port " + port);
    }
}

props.load(in) loads all the key-value pairs, then getProperty retrieves their values.

Configuration with YAML

The YAML Format

YAML is easier to read for nested structures and is the standard in Spring Boot:

application.yml file
app:
  name: BelajarJava
server:
  port: 8080
db:
  url: jdbc:postgresql://localhost:5432/toko
  pool-size: 10

YAML uses indentation to show hierarchy — clearer for complex configuration.

Reading YAML with Jackson

YAML can be read with Jackson Dataformat YAML:

Add Jackson YAML
mvn dependency:get -Dartifact=com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.17.2
Reading a YAML file
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.nio.file.*;
 
public class BacaYaml {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        JsonNode node = mapper.readTree(Path.of("application.yml").toFile());
        System.out.println(node.get("app").get("name").asText());
    }
}

Environment Variables

Why Environment Variables

Environment variables separate configuration from code completely and are ideal for sensitive values or values that differ between environments. Set them from the terminal:

Set environment variables
export DB_URL="jdbc:postgresql://localhost:5432/toko"
export APP_PORT=8080

Reading with System.getenv

Read environment variables in Java:

Reading environment variables
public class BacaEnv {
    public static void main(String[] args) {
        String dbUrl = System.getenv("DB_URL");
        String port = System.getenv().getOrDefault("APP_PORT", "8080");
        System.out.println("DB: " + dbUrl + " port: " + port);
    }
}

System.getenv("DB_URL") retrieves the variable value, and getOrDefault provides a default value when it is not found.

Multiple Environment Configuration

Profiles in Spring

Modern libraries such as Spring support profiles for multiple environments: dev, staging, and prod. Create a separate file per environment:

Configuration files per environment
application.yml            -> konfigurasi umum
application-dev.yml        -> konfigurasi development
application-prod.yml       -> konfigurasi produksi

Activating a Profile

Activate the profile when running the application:

Run with a profile
java -jar aplikasi.jar --spring.profiles.active=prod

With profiles, common configuration goes in the main file, and only the differences are overridden per environment.

Best Practices for Secrets and Credentials

Do Not Commit Secrets to the Repository

Secrets such as database passwords and API keys must not enter the repository. Separate public and secret configuration. Gitignore files that contain secrets:

Ignore secret files
echo ".env" >> .gitignore
echo "application-secret.yml" >> .gitignore

Secrets in Production

In production, do not store secrets in configuration files. Use environment variables, a secret manager such as Vault or AWS Secrets Manager, or the secret features of your cloud platform. The principle: the code is the same for all environments, secrets come from outside at runtime.

Warning

Never write passwords, API keys, or tokens in source code. Use environment variables or a secret manager, and make sure files that contain secrets are in .gitignore.

Closing

Episode 11 covers application configuration: properties and YAML files, environment variables, java.util.Properties and modern libraries such as Spring, multiple environment configuration with profiles, and best practices for storing secrets and credentials.

Key takeaways:

  • Properties use the key=value format; YAML for nested structures.
  • java.util.Properties loads files via props.load().
  • Environment variables separate configuration from code.
  • Spring profiles handle multiple environments with separate files.
  • Never commit secrets to the repository.
  • Production secrets are stored in environment variables or a secret manager.

In the next episode, episode 12, we will discuss basic networking and HTTP client — socket programming with java.net.Socket and ServerSocket, the modern HTTP API with java.net.http.HttpClient, making GET and POST requests, consuming REST APIs, and asynchronous requests with CompletableFuture. Time to talk to other services!

Learning Java - Configuration & Environment Variables | Learn Java