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 overridesProperty 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_dev2. 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!
4. Environment Strategy Matrix
| Concern | Development (dev) | Testing (test) | Production (prod) |
|---|---|---|---|
| Database Target | localhost:3306/student_dev | In-memory H2 / Test DB | Dedicated Prod Cluster |
| Server Port | 8081 | 8082 | Platform Default (8080) |
| Hibernate DDL | update | create-drop | validate / Flyway migrations |
| Logging Level | DEBUG | INFO | WARN / ERROR |
| Profile Activation | spring.profiles.active=dev | SPRING_PROFILES_ACTIVE=test | SPRING_PROFILES_ACTIVE=prod |
❓ Knowledge Check
What is the primary benefit of activating Spring profiles externally via environment variables in production?
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?
Configuration Binding: @Value vs @ConfigurationProperties
Master Spring property binding, single property injection with @Value, type-safe grouped binding with @ConfigurationProperties, and database blueprints.
Conditional Beans with @Profile & Production Patterns
Master conditional Spring bean registration using @Profile, resolving bean ambiguity, implementation switching, and production configuration anti-patterns.