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

a simple example for jdbc PreparedStatement

a simple example for PreparedStatement package basics.in.java.blogspot.in; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { private static final String USERNAME="root"; private static final String PASSWORD=""; private static final String CONN_STRING="jdbc:mysql://localhost/basicsinjavablogspot"; public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection conn=null; Statement stmt=null; ResultSet rs=null; try { conn= DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD); System.out.println("database connection successful"); //stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); String sql="select * fr...

Server-Side Pagination with React-Table and Spring Boot JPA with H2 Database

Pagination is a common technique used to split large amounts of data into smaller, more manageable chunks. With server-side pagination, data is retrieved from the server in smaller batches, reducing the amount of data transferred over the network and improving application performance. React-Table provides a wide range of built-in features such as sorting, filtering, pagination, row selection, and column resizing. These features can be easily configured and customized to fit specific requirements. For example, you can customize the sorting behavior to handle multiple sorting criteria, or you can add custom filters to the table to handle complex data filtering scenarios. Additionally, React-Table provides a flexible API that allows developers to extend its functionality with custom hooks, plugins, and components. This means that you can easily add custom functionality to the table, such as exporting data to CSV or integrating with external data sources. In terms of styl...