25. Spring Profiles & Environment Decoupling

Profile-Specific Configuration & Activation

Master Spring profiles, profile-specific files (application-dev.yml, application-prod.yml), activation mechanisms, and build-once-deploy-anywhere principles.

Profile-Specific Configuration & Activation

Spring Profiles allow developers to segregate parts of application configuration and make them available only in specific environments (e.g. dev, test, staging, prod).


1. Profile-Specific Configuration Files

Spring Boot automatically scans for profile-specific configuration files matching the naming convention application-{profile}.yml or application-{profile}.properties.

PROJECT CONFIGURATION LAYOUT:
src/main/resources/
├── application.yml          ──► Shared base configuration (common across ALL environments)
├── application-dev.yml      ──► Development environment overrides
├── application-test.yml     ──► Test environment overrides
└── application-prod.yml     ──► Production environment overrides

Property Override Priority

When a profile is activated, settings in application-{profile}.yml override matching settings in application.yml:

# application.yml (Base Defaults)
server:
  port: 8080
spring:
  application:
    name: student-management-app
# application-dev.yml (Dev Overrides)
server:
  port: 8081      # OVERRIDES base port 8080!
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/student_dev

2. Profile Activation Mechanisms

Spring Boot provides four primary mechanisms to activate profiles:

PROFILE ACTIVATION MECHANISMS:
├── 1. Environment Variable  ──► export SPRING_PROFILES_ACTIVE=prod (RECOMMENDED FOR PROD)
├── 2. Command-Line Argument ──► java -jar app.jar --spring.profiles.active=prod
├── 3. Maven Command         ──► mvn spring-boot:run -Dspring-boot.run.profiles=dev
└── 4. In-file Property       ──► spring.profiles.active=dev (LOCAL DEV ONLY)

3. Production Deployment Rule: Build Once, Deploy Anywhere

Production Golden Rule: Build the packaged application artifact (app.jar) ONCE. Never rebuild code or edit source configuration files merely to deploy to a different environment. Activate profiles externally at runtime!

SPRING_PROFILES_ACTIVE=dev SPRING_PROFILES_ACTIVE=test SPRING_PROFILES_ACTIVE=prod Java Source Code mvn clean package Single Executable app.jar Dev Server (Local DB, Port 8081) CI/CD Server (H2 DB, Port 8082) Kubernetes Cluster (Prod DB, Port 8080)

4. Environment Strategy Matrix

ConcernDevelopment (dev)Testing (test)Production (prod)
Database Targetlocalhost:3306/student_devIn-memory H2 / Test DBDedicated Prod Cluster
Server Port80818082Platform Default (8080)
Hibernate DDLupdatecreate-dropvalidate / Flyway migrations
Logging LevelDEBUGINFOWARN / ERROR
Profile Activationspring.profiles.active=devSPRING_PROFILES_ACTIVE=testSPRING_PROFILES_ACTIVE=prod

❓ Knowledge Check

Knowledge Check

What is the primary benefit of activating Spring profiles externally via environment variables in production?

Knowledge Check

If server.port=8080 is defined in application.yml and server.port=8081 is defined in application-dev.yml, which port is used when the dev profile is active?

On this page