Spring Boot
Configuration Files
External Configuration
Spring Framework
Java Development

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

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:

properties
1# application.properties
2server.port=8080
3spring.datasource.url=jdbc:mysql://localhost/test
4spring.datasource.username=root
5spring.datasource.password=root

Similarly, the equivalent YAML file:

yaml
1# application.yml
2server:
3  port: 8080
4spring:
5  datasource:
6    url: jdbc:mysql://localhost/test
7    username: root
8    password: root

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.properties
  • application-prod.properties

In your main configuration file (application.properties), you can specify which profile to use:

properties
# application.properties
spring.profiles.active=dev

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):

  1. Command line arguments
  2. JNDI attributes from java:comp/env
  3. Java System properties (System.getProperties())
  4. OS environment variables
  5. A RandomValuePropertySource for syntax generator purposes
  6. Profile-specific application properties (like application-dev.properties)
  7. 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:

java
1@Autowired
2private Environment env;
3
4public void printDatabaseConfigs() {
5    String dbUrl = env.getProperty("spring.datasource.url");
6    System.out.println("Database URL: " + dbUrl);
7}

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 FeatureDescription
Default Config Filesapplication.properties or application.yml in classpath or filesystem
Profile-Specific Configurationapplication-{profile}.properties for setting profile-based configurations
@Value AnnotationInjects environment properties directly into beans
Configuration HierarchyCommand line args > System variables > Profile properties > File properties
Dynamic Configuration with EnvUse Environment to programmatically access and modify config properties
Precedence of Config FilesProfile-specific properties override defaults
Additional Config LocationsSpecify 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
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.