Spring Boot and multiple external configuration files
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction to Spring Boot Configuration
Spring Boot is a widely used framework in the Java ecosystem, celebrated for its ability to simplify the development of new Java applications. One of the core features that make Spring Boot unique is its emphasis on convention over configuration, especially evident in how it handles configuration files. The Spring Boot framework allows for multiple external configuration files that can be customized for various environments and used in concert to support complex application requirements.
Configuration in Spring Boot
Spring Boot achieves seamless configuration management through its extensive environment abstraction. The environment consists of multiple property sources, such as configuration files, environment variables, and command-line arguments. Spring Boot’s flexible configuration capabilities enable developers to easily inject custom or external configurations into their applications.
Key Configuration Files
The key configuration files used in Spring Boot projects include:
application.properties: A standard properties file where you can define a variety of application-level settings.application.yml: A YAML file used as an alternative toapplication.propertiesto define settings in a hierarchical way.- Profile-specific properties: Files like
application-dev.propertiescan be used to define properties specific to certain deployment environments (dev, staging, prod).
Employing Multiple Configuration Files
Externalized Configuration
Spring Boot supports externalized configuration, allowing properties to be placed outside of your jar file instead of being bundled inside. This enables environments to override configuration without modifying the jar file or rebuilding it.
Configuration Priority
When using multiple configuration files, Spring Boot establishes a precedence order for these configurations. Here is the order in which Spring Boot considers configuration properties:
- Command line arguments: Highest priority, allowing runtime configuration.
application-dev.yml,application-prod.properties: Profile-specific configurations.application.yml/application.properties: Default baselayer configuration.- Environment variables: System environment level configurations.
- System properties: Parameters set using JVM arguments.
- Config data files: Additional exterior configuration files.
- Random values: For example,
random.values. - Application defaults: Shipping defaults in Spring Boot itself.
Example: Using Multiple Configuration Files
Suppose you have the following configuration files in your project:
application.yml
application-dev.yml
In this example, the developer can start the application using the dev profile by setting spring.profiles.active=dev. Spring Boot will load application-dev.yml and override the server port and datasource URL specified in the default application.yml.
Defining Additional Properties Files
You can also define additional properties files and include them in your Spring Boot application. This is done with the @PropertySource annotation. For example:
Summarizing Configuration Scenarios
The following table summarizes key aspects of Spring Boot Configuration:
| Configuration Aspect | Description | Example |
| Default Property File | Base configuration | application.properties / .yml |
| Profile-Specific Configurations | Environment-specific settings | application-dev.properties / .yml |
| Command Line Arguments | Highest priority for properties | --server.port=9090 |
| Environment Variables | System-wide property settings | DATABASE_URL |
| Externalized Configurations | Files outside the jar | config/application-external.properties |
| Additional Property Sources | Additional settings using @PropertySource | additional-config.properties |
Conclusion
Spring Boot's approach to managing application configuration through multiple external files ensures a high level of flexibility and control, especially in environments where applications need to dynamically adjust settings without necessitating redeployment. Understanding and effectively applying these principles can significantly enhance your application development and deployment strategies.
Remember, configurations in Spring Boot are prioritized based on their source, allowing customizable and scalable solutions for nearly every application environment.
Related reading
- Spring Boot and SQLite
- Spring Boot Application as a Daemon Service?
- Spring Boot application as a Service
- Spring Boot application gives 404 when deployed to Tomcat but works with embedded server
- Spring Boot application in eclipse, the Tomcat connector configured to listen on port XXXX failed to start
- Spring boot application.properties maven multi-module projects
- Spring Boot application.properties value not populating
- Spring boot applications consume 100 CPU at startup

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.