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.
Spring Boot is a powerful, convention-over-configuration framework for creating stand-alone, production-grade Spring-based applications. One of the features that makes Spring Boot particularly popular is its flexible configuration management system. In Spring Boot applications, properties can be defined in multiple external configuration files. This capability allows developers to dynamically manage their application configurations based on different environments, profiles, or services.
External Configuration Files in Spring Boot
Spring Boot's ability to utilize external configuration files provides a high degree of flexibility and customizability. Typically, these files are in YAML or .properties formats and can be located on the classpath or the file system. The configuration properties can be loaded into the application context, making them available across the entire lifecycle of the application.
Basics of External Configuration
In Spring Boot, the @Value annotation or the Environment abstraction can be used to inject values from configuration files directly into your beans. The configuration system is built on top of the underlying Spring Framework features, enriched with additional tools specific to Spring Boot.
Default Configuration Files
When you build a Spring Boot application, the default configuration file is typically named application.properties or application.yml. By default, Spring Boot looks for these files in the following locations:
- Classpath root:
/config - Classpath root:
/ - Filesystem:
/config
Example Structure
Here's an example of a simple application.properties:
Similarly, the equivalent YAML file:
Using Multiple Configuration Files
In many cases, your application needs to support different configurations based on the environment or profile. Spring Boot introduces profiles, which allow you to define separate configuration files for different environments like development, testing, and production.
Profile-Specific Configuration
To define profile-specific configuration files, you use the naming convention application-{profile}.properties or application-{profile}.yml. For example, for development and production environments, you might have:
application-dev.propertiesapplication-prod.properties
In your main configuration file (application.properties), you can specify which profile to use:
When the application starts, Spring Boot automatically loads application-dev.properties along with the main application.properties. Any overlapping keys will be overridden by the profile-specific ones.
Order of Precedence
When multiple sources are available for configuration, Spring Boot determines their precedence based on the following order (highest to lowest):
- Command line arguments
- JNDI attributes from
java:comp/env - Java System properties (
System.getProperties()) - OS environment variables
- A
RandomValuePropertySourceforsyntax generator purposes - Profile-specific application properties (like
application-dev.properties) - Packaged inside your jar
application.properties
Dynamic Configuration with Environment
Spring Boot's Environment abstraction can be used programmatically to access configuration properties. For example:
Using the Environment object, you can also resolve property values dynamically at runtime, catering to complex application needs.
Summary Table
Here's a summary table highlighting the key points of Spring Boot external configuration:
| Key Feature | Description |
| Default Config Files | application.properties or application.yml in classpath or filesystem |
| Profile-Specific Configuration | application-{profile}.properties for setting profile-based configurations |
| @Value Annotation | Injects environment properties directly into beans |
| Configuration Hierarchy | Command line args > System variables > Profile properties > File properties |
Dynamic Configuration with Env | Use Environment to programmatically access and modify config properties |
| Precedence of Config Files | Profile-specific properties override defaults |
| Additional Config Locations | Specify additional locations with spring.config.location |
By efficiently managing configurations as external files, Spring Boot ensures that your applications are robust, adaptable, and tailored to their operating environments. This helps developers seamlessly transition solutions from development to production while maintaining clarity and control over environment-specific settings.
Further Enhancements
- Encrypted Properties: Consider using tools like Jasypt for encrypted properties in external configuration files.
- Externalized Secrets: Employ platforms like HashiCorp Vault or AWS Secrets Manager for managing application secrets and sensitive data.
- Centralized Configuration Management: Tools like Spring Cloud Config Server can be used for centralized configuration management across distributed applications.
Spring Boot's approach to configuration through external files provides a structured and flexible pathway for managing application settings, thus facilitating more efficient application development and deployment strategies.
Related reading
- Spring Boot and multiple external configuration files
- 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

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.