Does application.yml support environment variables?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In modern software development, configuration management plays a crucial role, allowing developers to manage application settings for different environments seamlessly. The Spring Framework, a popular choice for building Java applications, offers a variety of configuration files, among which application.yml (or application.yaml) is renowned for its hierarchical data representation and human-readability. A common question among developers is whether application.yml supports environment variables. The answer is a resounding yes, and this capability enhances the flexibility and adaptability of your applications significantly.
Understanding YAML Configuration in Spring Boot
Spring Boot applications often use application.yml or application.properties to manage settings. The YAML format is preferred for its clean syntax and ability to represent complex data structures succinctly. Here's a simple example of what an application.yml file might look like:
In this example, the application listens on port 8080, and the database connection settings are defined.
Environment Variables in application.yml
Environment variables are key to configuring applications dynamically without changing the source code. Spring Boot facilitates the use of environment variables within application.yml through the placeholder syntax: ${VARIABLE_NAME}. This allows an application to retrieve the value of an environment variable and use it within the YAML configuration.
Example of Using Environment Variables
Consider integrating environment variables for database configuration. You can define the same application.yml as follows:
With this setup, you can manage different database configurations across environments (development, staging, production) by simply setting the corresponding environment variables: DB_URL, DB_USERNAME, and DB_PASSWORD.
Setting Environment Variables
To set environment variables, you can utilize different methods based on your operating system or runtime environment. Here are a few common approaches:
- Command Line (Unix-based systems):
- Windows Command Prompt:
- Docker Compose:Within a
docker-compose.yml, you can specify environment variables:
Precedence of Environment Variables
When using environment variables in Spring Boot, it’s important to understand the order of precedence. Spring Boot resolves properties in the following order, from highest to lowest precedence:
- Command line arguments
- Environment variables
application.properties(orapplication.yml) located in the current directory- Profile-specific configuration files
- Default configuration files packaged inside the application
This means that if an environment variable is set, it will override the same property specified in application.yml.
Advanced Usage: Profiles and Conditional Logic
Spring Boot's profile-specific configuration allows for managing environment-specific properties effectively. You may define separate YAML files for each profile, such as application-dev.yml and application-prod.yml, and include the placeholder syntax for environment variables in these files as well.
Moreover, you can programmatically access these variables within your application code using the Environment class provided by Spring:
Table: Key Concepts of Environment Variable Integration
| Feature | Description |
| Placeholder Syntax | ${VARIABLE_NAME} syntax to incorporate environment values in YAML config. |
| Setting Env Variables | Use terminal or CI/CD pipelines to set environment-specific variables. |
| Order of Precedence | Command line > Env variables > application.yml > Profile-specific files. |
| Profile-Specific Configs | Separate YAML files per profile to manage environment differences. |
| Programmatic Access | Use Spring's Environment class in Java code for property access. |
Conclusion
The support for environment variables in application.yml significantly empowers developers to build flexible and environment-agnostic applications. By leveraging this feature, your Spring Boot applications can easily handle configuration variations across different environments without code changes, thus promoting clean and maintainable codebases. When combined with best practices such as profile-specific configurations and an understanding of the Spring Boot property-sources precedence, environment variable integration ensures a robust and adaptive configuration management strategy.

