Spring Boot
Configuration Files
External Configuration
Java
Application Setup

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.

Browse interview questions

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:

  1. application.properties: A standard properties file where you can define a variety of application-level settings.
  2. application.yml: A YAML file used as an alternative to application.properties to define settings in a hierarchical way.
  3. Profile-specific properties: Files like application-dev.properties can 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:

  1. Command line arguments: Highest priority, allowing runtime configuration.
  2. application-dev.yml, application-prod.properties: Profile-specific configurations.
  3. application.yml / application.properties: Default baselayer configuration.
  4. Environment variables: System environment level configurations.
  5. System properties: Parameters set using JVM arguments.
  6. Config data files: Additional exterior configuration files.
  7. Random values: For example, random.values.
  8. 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

yaml
1server:
2  port: 8080
3spring:
4  datasource:
5    url: jdbc:mysql://localhost:3306/mydb
6    username: user
7    password: pass

application-dev.yml

yaml
1server:
2  port: 8081
3spring:
4  datasource:
5    url: jdbc:mysql://dev-server:3306/mydb_dev

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:

java
1@PropertySource("classpath:additional-config.properties")
2public class AdditionalConfig {
3    // Your beans here
4}

Summarizing Configuration Scenarios

The following table summarizes key aspects of Spring Boot Configuration:

Configuration AspectDescriptionExample
Default Property FileBase configurationapplication.properties / .yml
Profile-Specific ConfigurationsEnvironment-specific settingsapplication-dev.properties / .yml
Command Line ArgumentsHighest priority for properties--server.port=9090
Environment VariablesSystem-wide property settingsDATABASE_URL
Externalized ConfigurationsFiles outside the jarconfig/application-external.properties
Additional Property SourcesAdditional settings using @PropertySourceadditional-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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.