Skip to main content

Configuring Multiple Profiles in a Single YAML File for Spring Boot Application

Spring Boot provides a convenient way to configure application properties using YAML files. In a typical scenario, you might have different configurations for different environments such as development, testing, and production. Instead of maintaining multiple YAML files for each profile, you can configure multiple profiles within a single YAML file. This approach simplifies the configuration management process. Let's see how to achieve this.

Step 1: Create a YAML Configuration File

First, create a YAML file (e.g., application.yml) in your Spring Boot project's resource directory. This file will contain the configuration properties for all the profiles you want to define.


spring:
  profiles:
    active: dev

logging:
  level:
    root: INFO
    com.example: DEBUG

# Configuration for the 'dev' profile
---
spring:
  profiles: dev

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

# Configuration for the 'prod' profile
---
spring:
  profiles: prod

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

In the above example, we have two profiles defined: dev and prod. The dev profile configures the database URL, username, and password for the development environment. Similarly, the prod profile configures these properties for the production environment.

Step 2: Activate a Specific Profile

By default, the dev profile is active as specified by spring.profiles.active property in the configuration file. You can change the active profile by setting the environment variable SPRING_PROFILES_ACTIVE to the desired profile name.

Step 3: Access Profile-Specific Properties

In your Spring Boot application, you can access the profile-specific properties using the @Value annotation or the @ConfigurationProperties annotation.

Using @Value Annotation


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {

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

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

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

  // ...
}
  

Using @ConfigurationProperties Annotation


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("database")
public class DatabaseProperties {

  private String url;
  private String username;
  private String password;

  // Getters and setters

  // ...
}
  

With the above configuration, you can inject the DatabaseProperties bean into your components and access the profile-specific properties using its getters.

Configuring multiple profiles in a single YAML file simplifies the management of configuration properties for different environments in a Spring Boot application. You can define profile-specific properties in the same file and easily switch between profiles using the spring.profiles.active property or environment variables.

Comments

Popular posts from this blog

How to Open a Project in IntelliJ IDEA from the Command Line: A Step-by-Step Guide

How to Open a Project in IntelliJ IDEA from the Command Line IntelliJ IDEA is a popular and powerful Integrated Development Environment (IDE) for Java development. You may have a project folder that you want to open in IntelliJ IDEA. In this tutorial, we will walk through the steps for opening a project in IntelliJ IDEA from the command line. Configure IntelliJ IDEA for Command Line Use Before we can open a project in IntelliJ IDEA from the command line, we need to make sure that it is properly configured. Here are the steps: Open IntelliJ IDEA and go to Tools > Create Command-line Launcher . Select the installation path and click OK . Make sure that the launcher is added to your system PATH variable. You can do this by opening a terminal or command prompt and typing: which idea If the output is the path to the IntelliJ IDEA launcher, then you're good to go! Open a Project in IntelliJ IDEA from the Command Line Open your terminal or command prompt. ...

A End to End ant Build xml snippet

A End to End ant Build xml snippet you could use for reference <?xml version="1.0" encoding="UTF-8"?> <project name="End to End Build" default="sendmail" basedir=".">     <target name="init">         <tstamp />         <property file="build.properties" />             <path id="classpath">             <pathelement path="${servlet.jar}" />             <pathelement path="${junit.jar}" />         </path>