Spring Boot Profile Feature: A Comprehensive Guide

1 min read

Spring Boot Profile Feature: A Comprehensive Guide

Spring Boot provides the Profile feature to configure different environments such as development, testing, and production. This feature helps in managing application settings efficiently without hardcoding environment-specific configurations.

What is a Spring Boot Profile?

A Spring Profile is a way to group application configurations and beans based on a specific environment. This allows the application to load only relevant configurations at runtime, making it easier to maintain environment-specific settings.

Defining Profiles in Spring Boot

Spring Boot allows defining profiles using:

  1. application.properties or application.yml
  2. @Profile annotation

1. Using application.properties or application.yml

Spring Boot supports different profile-specific property files, which are named as:

  • application-dev.properties
  • application-test.properties
  • application-prod.properties

Example:

application-dev.properties

1
2
3
4
server.port=8081
datasource.url=jdbc:mysql://localhost:3306/devdb
datasource.username=devuser
datasource.password=devpassword

application-prod.properties

1
2
3
4
server.port=8080
datasource.url=jdbc:mysql://localhost:3306/proddb
datasource.username=produser
datasource.password=prodpassword

To activate a specific profile, add the following property in application.properties:

1
spring.profiles.active=dev

Or set it via command-line:

1
java -jar myapp.jar --spring.profiles.active=prod

2. Using @Profile Annotation

Spring allows defining beans specific to a profile using the @Profile annotation.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
 
@Configuration
public class AppConfig {
 
    @Bean
    @Profile("dev")
    public DataSource devDataSource() {
        return new DataSource("jdbc:mysql://localhost:3306/devdb", "devuser", "devpassword");
    }
 
    @Bean
    @Profile("prod")
    public DataSource prodDataSource() {
        return new DataSource("jdbc:mysql://localhost:3306/proddb", "produser", "prodpassword");
    }
}

Switching Profiles Programmatically

Profiles can also be switched dynamically in Java:

1
2
3
4
5
6
7
8
9
10
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
 
public class ProfileSwitcher {
    public static void main(String[] args) {
        Environment env = new StandardEnvironment();
        ((StandardEnvironment) env).setActiveProfiles("test");
        System.out.println("Active Profile: " + env.getActiveProfiles()[0]);
    }
}

Using Spring Boot Profiles in application.yml

Spring Boot also supports YAML-based profile configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
server:
  port: 8081
---
spring:
  profiles: prod
server:
  port: 8080

Conclusion

Spring Boot’s Profile feature provides a clean and efficient way to manage multiple environments with minimal effort. By using profile-specific property files, annotations, or programmatic configurations, developers can streamline environment management and ensure smooth application deployment across different settings.

🤞 Never miss a story from us, get weekly updates to your inbox!