What are Profiles in Spring?
Profiles vs Feature Flags: if you need to enable/disable functionality in production WITHOUT restart - use Feature Flags (Unleash, LaunchDarkly). If different infrastructure for...
Junior Level
Profiles - a way to use different settings for different environments (dev, test, prod).
# application.yaml (common settings)
server.port: 8080
# application-dev.yaml (for development)
spring.datasource.url: jdbc:h2:mem:dev
# application-prod.yaml (for production)
spring.datasource.url: jdbc:postgresql://prod-db:5432/mydb
Activation:
java -jar app.jar --spring.profiles.active=prod
Middle Level
Activation Methods
# 1. Command line
--spring.profiles.active=prod
# 2. Environment variable
SPRING_PROFILES_ACTIVE=prod
# 3. JVM parameter
-Dspring.profiles.active=prod
# 4. In tests
@ActiveProfiles("test")
@Profile on Beans
@Bean
@Profile("dev")
public DataSource devDataSource() { ... }
@Bean
@Profile("prod")
public DataSource prodDataSource() { ... }
Logical Expressions
@Profile("prod & !cloud") // Prod, but not in cloud
// Spring 5.1+ supports logical operators in @Profile: &, |, !
@Profile("dev | test") // Dev or test
@Profile("prod & (mysql | postgres)") // Complex conditions
Senior Level
Profile Groups
spring:
profiles:
group:
"highload": "cache, async, metrics"
# Activating highload -> enables cache, async, metrics
Profile-specific Imports (Boot 2.4+)
# application.yaml
server.port: 8080
---
spring.config.activate.on-profile: dev
server.port: 9090
---
spring.config.activate.on-profile: prod
server.port: 80
Profiles vs Feature Flags
Profiles:
-> Infrastructure (DB, queues)
-> Requires restart
Feature Flags:
-> Business logic
-> Changed on the fly
-> Do not confuse them!
Profiles vs Feature Flags: if you need to enable/disable functionality in production WITHOUT restart - use Feature Flags (Unleash, LaunchDarkly). If different infrastructure for dev/test/prod - Profiles.
Cloud-Native
Kubernetes ConfigMaps -> replace profiles
Spring Cloud Config -> external config server
HashiCorp Vault -> secrets
Profiles -> bridge to external configs
Production Experience
Real scenario: forgot to activate prod
Application started with default profile
-> Connected to dev DB!
-> Data loss!
Solution:
-> Require profile via validation
-> ConfigMaps in K8s
Diagnostics
# Active profiles:
/actuator/env -> activeProfiles
# Log on startup:
"The following 1 profile is active: prod"
# Programmatically:
@Autowired Environment env;
env.getActiveProfiles();
Best Practices
- Do not store secrets in application.yaml
- Require profile for prod -> validation
- Profile Groups -> grouping
- Feature Flags -> for business logic
- Cloud Config -> external management
- @ActiveProfiles -> for tests
Summary for Senior
- Profiles -> different configuration for environments
- Source priority (highest to lowest): CLI arguments > JVM -D parameters > environment variables > application-{profile}.properties > application.properties
- Groups -> profile aggregation
- Profile vs Feature Flags -> infrastructure vs business
- Cloud-Native -> ConfigMaps, Vault
- Validation -> require profile for prod
Interview Cheat Sheet
Must know:
- Profiles - mechanism for different configuration per environment (dev, test, prod)
- Activation: –spring.profiles.active, SPRING_PROFILES_ACTIVE, JVM -D, @ActiveProfiles in tests
- @Profile on @Bean methods and classes - bean created only when profile is active
-
Logical expressions in @Profile (Spring 5.1+): “prod & !cloud”, “dev test” - Profile Groups (Spring Boot 2.4+): one profile activates a group - spring.profiles.group
- Multi-document YAML: multiple profiles in one file via spring.config.activate.on-profile
- Profiles != Feature Flags: profiles - infrastructure (restart), flags - business logic (on the fly)
Common follow-up questions:
- How to activate multiple profiles? - Comma-separated: –spring.profiles.active=prod,cache,async.
- What happens if no profile is activated? - Only application.yaml is used (default settings) - dangerous for prod.
- How do Profiles differ from Feature Flags? - Profiles require restart and change infrastructure; Feature Flags toggle business logic on the fly.
- How to guarantee startup only with the right profile? - Validation: check env.getActiveProfiles() on startup and throw error if prod without profile.
Red flags (DO NOT say):
- “Profiles can be changed without restart” - no, activation only on startup.
- “Feature Flags are the same as Profiles” - no, flags change business logic on the fly.
- “Secrets can be stored in application-prod.yaml” - no, use Vault, K8s Secrets, CI/CD variables.
- “Profile groups replace individual profiles” - no, groups are an alias for a set of profiles.
Related topics:
- [[22. What is a starter in Spring Boot]]
- [[21. How SpringBootApplication works]]
- [[24. What is a Configuration class]]
- [[27. What to do if there are multiple beans of the same type]]