Configuration management is a critical aspect of building robust applications. Spring Boot simplifies configuration handling by supporting various file formats, including YAML. YAML (YAML Ain’t Markup Language) offers a human-readable and concise syntax, making it an excellent choice for configuring Spring Boot applications. In this article, we will explore how to leverage YAML property files in Spring Boot, providing practical examples along the way.

  1. Understanding YAML Property Files:
    YAML property files allow you to define application properties using a structured and readable format. The YAML syntax consists of key-value pairs, nested data structures, and lists, making it flexible for representing complex configurations.
  2. Creating YAML Property Files:
    To start using YAML property files in your Spring Boot application, create a new file in the src/main/resources directory and name it application.yml or application.yaml. Spring Boot automatically picks up this file as the default configuration source.
  3. Basic Property Configuration:
    YAML property files use indentation and colons to define properties and their values. Here’s an example of a basic YAML property file:
server:
  port: 8080
  context-path: /myapp

database:
  url: jdbc:mysql://localhost:3306/mydb
  username: myuser
  password: mypassword
  1. Accessing YAML Properties:
    To access YAML properties in your Spring Boot application, you can use the @Value annotation or @ConfigurationProperties annotation.

a. Using the @Value Annotation:

@Value("${server.port}")
private int serverPort;

@Value("${database.url}")
private String databaseUrl;

b. Using the @ConfigurationProperties Annotation:

@ConfigurationProperties(prefix = "server")
public class ServerProperties {
    private int port;
    private String contextPath;

    // Getters and setters
}
  1. Nested Properties:
    YAML property files support nested structures, allowing you to organize your configurations in a hierarchical manner. Here’s an example:
app:
  name: MyApp
  version: 1.0.0
  author:
    name: John Doe
    email: johndoe@example.com

To access nested properties, you can use dot notation or the @ConfigurationProperties annotation with nested classes.

  1. Lists and Arrays:
    YAML property files can handle lists and arrays efficiently. Here’s an example:
fruits:
  - apple
  - banana
  - orange

To access a list or array property, you can use the @Value annotation with the List or String[] type, or the @ConfigurationProperties annotation with a List or Array field.

  1. Profiles in YAML Property Files:
    YAML property files support profiles, allowing you to define different configurations for different environments. Create separate YAML files for each profile, such as application-dev.yml and application-prod.yml, and specify the active profile using the spring.profiles.active property in your application.yml file.
spring:
  profiles:
    active: dev
  1. Externalizing YAML Property Files:
    You can externalize YAML property files by specifying the file location using the spring.config.name and spring.config.location properties.
java -jar myapp.jar --spring.config.name=myapp --spring.config.location=file:/path/to/config/

Multiple Profiles in a Single File in Spring Boot

Managing application configurations efficiently is crucial for building flexible and maintainable systems. Spring Boot provides robust support for configuration management using YAML property files. In this article, we will explore how to utilize YAML property files with multiple profiles defined within a single file in Spring Boot, providing practical examples and a step-by-step guide.

  1. Understanding YAML Property Files and Multiple Profiles:
    YAML property files offer a clean and readable format for configuring Spring Boot applications. Multiple profiles allow you to define different sets of properties for various environments or deployment scenarios. By combining these features within a single YAML file, you can streamline configuration management and avoid maintaining separate files for each profile.
  2. Creating a YAML Property File with Multiple Profiles:
    To begin, create a YAML property file, such as application.yml or application.yaml, in the src/main/resources directory. This single file will contain the configurations for all the profiles you want to define.
  3. Defining Profile-Specific Properties:
    To specify profile-specific properties within the single YAML property file, utilize YAML’s syntax for nesting and separating profiles. Here’s an example of how to define different database configurations for the dev and prod profiles:
spring:
  profiles:
    active: dev

---

spring:
  profiles: dev
  database:
    url: jdbc:mysql://localhost:3306/devdb
    username: devuser
    password: devpassword

---

spring:
  profiles: prod
  database:
    url: jdbc:mysql://localhost:3306/proddb
    username: produser
    password: prodpassword

In the above example, the spring.profiles.active property is set to dev, indicating that the dev profile is active by default. Each profile-specific configuration is encapsulated within --- separators.

  1. Accessing Profile-Specific Properties:
    To access the profile-specific properties in your Spring Boot application, you can use the @Value annotation or the @ConfigurationProperties annotation.

a. Using the @Value Annotation:

@Value("${spring.database.url}")
private String databaseUrl;

@Value("${spring.database.username}")
private String databaseUsername;

@Value("${spring.database.password}")
private String databasePassword;

b. Using the @ConfigurationProperties Annotation:

@ConfigurationProperties(prefix = "spring.database")
public class DatabaseProperties {
    private String url;
    private String username;
    private String password;

    // Getters and setters
}
  1. Switching between Profiles:
    To switch between different profiles defined within the same YAML property file, you can modify the spring.profiles.active property value in your application.
  • By setting the property in the application.properties file:
spring.profiles.active=prod
  • By passing the active profile as a command-line argument:
java -jar your-app.jar --spring.profiles.active=prod
  • By setting the SPRING_PROFILES_ACTIVE environment variable:
export SPRING_PROFILES_ACTIVE=prod
  1. Default Properties:
    In addition to profile-specific properties, you can define default properties within the same YAML file. These default properties will be used when no profile-specific properties are available.
  2. Property Precedence:
    Spring Boot follows a specific order of precedence when resolving properties, considering both profile-specific and default properties:
  3. Profile-specific properties (highest precedence)
  4. Default properties
  5. Command-line arguments
  6. Environment variables
  7. System properties (lowest precedence)

Conclusion:
Utilizing a single YAML property file with multiple profiles simplifies configuration management in Spring Boot applications


Discover more from GhostProgrammer - Jeff Miller

Subscribe to get the latest posts sent to your email.

By Jeffery Miller

I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.