All topics
library
intermediate

Properties & Configuration Loading

Load configuration from .properties files, system properties, and environment variables in the right order.

Java applications need configuration for database URLs, feature flags, timeouts, etc. Multiple sources exist, with a typical precedence order:

Configuration loading = a stack of transparent sheets. Each sheet (source) has some settings filled in. You read from top to bottom — the first sheet with a value wins. Command-line is on top, defaults are on the bottom.

Key Concepts

1
1. Command-line arguments (-Dkey=value) 2. Environment variables (System.getenv()) 3. System properties (System.getProperty()) 4. Application properties files (classpath or filesystem) 5. Default values in code
2
java.util.Properties: a Hashtable<String,String> subclass that loads key=value pairs from .properties files. Supports = and : delimiters, # comments, and Unicode escapes.
3
Loading: - From classpath: getClass().getResourceAsStream("/app.properties") - From filesystem: new FileInputStream("config/app.properties") - System properties: System.getProperty("key", "default") - Environment: System.getenv("DATABASE_URL")
4
Spring Boot: uses application.properties / application.yml with profile support (application-dev.yml), @Value injection, @ConfigurationProperties binding, and externalized configuration.
5
Best practices: - Never hardcode secrets — use environment variables or secret managers - Use sensible defaults with override capability - Validate configuration at startup (fail-fast) - Document all configuration keys