Spring Boot Profiles

🚀 Introduction to Spring Boot Profiles

Spring Boot Profiles allow applications to load different configurations based on the environment (e.g., development, testing, production).

Why Use Spring Boot Profiles?
Manage Environment-Specific Settings (Database, API Keys, Logging).
Easily Switch Configurations without modifying code.
Use Different Beans for Different Environments (@Profile).
Avoid Hardcoding Environment Variables.

📌 In this guide, you’ll learn:
How Spring Boot Profiles Work.
Using application.properties and application.yml for Profiles.
How to Load Beans Conditionally Using @Profile.
How to Switch Between Profiles.

1️⃣ How Do Spring Boot Profiles Work?

Spring Boot Profiles allow you to define multiple environment-specific configurations and activate them dynamically.

📌 Common Profiles Used in Projects:

Profile Name Purpose
dev Used in local development
test Used for automated testing
staging Pre-production environment
prod Live production environment

2️⃣ Defining Profiles in application.properties

By default, Spring Boot loads application.properties, but we can create environment-specific files like:

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

📌 Example: application-dev.properties (Development Settings)

server.port=8081
spring.datasource.url=jdbc:h2:mem:devdb
logging.level.root=DEBUG

📌 Example: application-prod.properties (Production Settings)

server.port=8080
spring.datasource.url=jdbc:mysql://prod-db-server:3306/proddb
logging.level.root=ERROR

🚀 Spring Boot will automatically pick the right file based on the active profile!

3️⃣ Using Profiles in application.yml

You can also define profiles using YAML format (application.yml).

📌 Example: application.yml with Multiple Profiles

server:
  port: 8080

spring:
  profiles:
    active: dev  # Default active profile

---
spring:
  profiles: dev
  datasource:
    url: jdbc:h2:mem:devdb
  logging:
    level:
      root: DEBUG

---
spring:
  profiles: prod
  datasource:
    url: jdbc:mysql://prod-db-server:3306/proddb
  logging:
    level:
      root: ERROR

Here, Spring Boot will load the configuration based on the active profile.

4️⃣ Activating Profiles in Spring Boot

Spring Boot does not load profiles automatically. You need to activate a profile using one of the following methods:

Method 1: Using spring.profiles.active in application.properties

📌 Set active profile in application.properties file:
spring.profiles.active=dev

Method 2: Using Command-Line Arguments

Run your Spring Boot app with a specific profile:
bash java -jar myapp.jar --spring.profiles.active=prod

Method 3: Using Environment Variables

Set the profile in the OS environment:

export SPRING_PROFILES_ACTIVE=prod

This method is useful for cloud deployments (AWS, Azure, Kubernetes).

5️⃣ Using @Profile to Load Beans Conditionally

You can define environment-specific beans using the @Profile annotation.

📌 Example: Different DataSource Beans for Dev and Prod

@Configuration
public class DataSourceConfig {

    @Bean
    @Profile("dev")
    public DataSource devDataSource() {
        System.out.println("Using Dev DataSource");
        return new HikariDataSource(); // H2 In-Memory DB
    }

    @Bean
    @Profile("prod")
    public DataSource prodDataSource() {
        System.out.println("Using Prod DataSource");
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://prod-db-server:3306/proddb");
        return dataSource;
    }
}

When the dev profile is active, it uses an H2 database. When prod is active, it connects to MySQL.

6️⃣ Testing Profile-Specific Beans

You can test which bean is being loaded by running:

mvn spring-boot:run -Dspring-boot.run.profiles=dev

Spring Boot logs will show:

Using Dev DataSource

7️⃣ Disabling a Profile-Specific Property

If you want to disable a property in a specific profile, you can use spring.config.activate.on-profile.

📌 Example: application.yml

spring:
  config:
    activate:
      on-profile: prod
  cache:
    enabled: false

This setting will only be applied in the prod profile.

🎯 Summary: What We Learned

Spring Boot Profiles allow environment-based configuration.
Use application-dev.properties, application-prod.properties for separate configurations.
Profiles can be activated via application.properties, command-line, or environment variables.
Use @Profile annotation to define beans specific to environments.
Spring Boot automatically loads the right configuration based on the active profile.

📢 Share this guide to help others understand Spring Boot Profiles! 🚀

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare