External configuration for spring-boot application
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
External configuration is one of Spring Boot’s most useful features because it lets the same application binary run in development, test, and production with different settings. Instead of recompiling code for each environment, you supply values from files, environment variables, or command-line arguments. The important part is understanding both where Spring Boot reads properties from and how those values are bound into your application.
Start with application.properties or YAML
Spring Boot automatically loads configuration from standard locations such as the classpath and common external config directories. You can use either .properties or YAML files.
A simple YAML example:
The equivalent properties file would be:
Spring Boot supports overriding packaged configuration with external files. In practice, that means you can ship default settings inside the jar and provide environment-specific values outside the jar at deployment time.
Bind Configuration with @ConfigurationProperties
For anything beyond a couple of simple values, @ConfigurationProperties is cleaner than scattering @Value throughout the codebase. It gives you structured, typed configuration and makes validation easier.
Then enable scanning and inject the properties bean:
This approach scales much better than reading raw strings from the environment in many places.
Use Environment Variables and Command-Line Overrides
Spring Boot’s property resolution order is designed so that later, more specific sources can override earlier defaults. In real deployments, environment variables and command-line flags are common override layers.
For example:
Spring Boot maps environment variables such as APP_NAME to app.name. Command-line arguments such as --app.feature-enabled=false have even higher precedence than file-based configuration.
That makes them useful for last-mile deployment tweaks, but it also means you should document them carefully. Mystery overrides from shell scripts are a common source of confusion.
Profiles and Imported Config
Profiles let you keep related settings grouped by environment. For example, application-prod.yml is loaded when the prod profile is active.
Run the application with:
Spring Boot also supports imported config data. That is useful when you need to pull settings from an external file path or platform-provided location:
This keeps sensitive or deployment-specific settings out of the packaged application.
Common Pitfalls
One common mistake is mixing too many configuration formats in the same project. Spring Boot supports both .properties and YAML, but consistency makes troubleshooting easier.
Another mistake is assuming an external file is loaded when it is actually in the wrong location or profile. If a property does not seem to apply, check active profiles and the property source order first.
Developers also often overuse @Value for grouped settings. It works, but it becomes fragile as configuration grows. Structured binding with @ConfigurationProperties is easier to test and refactor.
Finally, do not treat external configuration as a secure secret store by itself. Environment variables and files can still leak. For sensitive values, combine external config with a proper secret-management system.
Summary
- Spring Boot supports external configuration from files, environment variables, and command-line arguments.
- Packaged defaults can be overridden by environment-specific external values.
- '
@ConfigurationPropertiesis the preferred way to bind grouped settings into typed Java classes.' - Profiles and
spring.config.importhelp organize environment-specific configuration. - Most configuration bugs come from misunderstanding precedence, active profiles, or file locations.

