Spring Boot Profiles – Detailed Explanation
Spring Boot Profiles allow you to define different configurations for different environments such as development, testing, staging, and production.
What are Spring Boot Profiles?
Profiles help you run the same codebase with different configurations without changing the application code.
- dev – Development environment
- test – Testing environment
- stage – Staging environment
- prod – Production environment
Why Do We Need Profiles?
| Environment | Typical Differences |
|---|---|
| Dev | Local DB, debug logs |
| Test | In-memory DB, mock services |
| Prod | Real DB, strict security |
Profiles provide clean separation, safer deployments, and easy environment switching.
How Spring Boot Profiles Work
Spring Boot loads configuration based on:
- Active profile
- Default profile
- Profile-specific configuration files
Common configuration files:
application.properties
application-dev.properties
application-test.properties
application-prod.properties
1. Defining Profiles Using Properties Files
Default Configuration
# application.properties
spring.application.name=my-app
Development Profile
# application-dev.properties
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/devdb
logging.level.root=DEBUG
Production Profile
# application-prod.properties
server.port=8080
spring.datasource.url=jdbc:mysql://prod-server:3306/proddb
logging.level.root=ERROR
2. Activating a Profile
Using application.properties
spring.profiles.active=dev
Using JVM Argument (Recommended for Production)
java -jar app.jar --spring.profiles.active=prod
Using Environment Variable
export SPRING_PROFILES_ACTIVE=test
Using IDE (VM Options)
-Dspring.profiles.active=dev
3. Using @Profile Annotation
You can enable or disable beans based on active profiles.
@Service
@Profile("dev")
public class DevEmailService implements EmailService {
}
@Service
@Profile("prod")
public class ProdEmailService implements EmailService {
}
Only the bean matching the active profile will be loaded.
4. Using @Profile with Configuration Classes
@Configuration
@Profile("test")
public class TestConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.build();
}
}
5. Default Profile
If no profile is explicitly set, Spring uses the default profile.
@Profile("default")
@Component
public class DefaultService {
}
6. Multiple Active Profiles
Spring Boot supports activating multiple profiles at once.
--spring.profiles.active=dev,swagger
Spring loads configuration files for all active profiles. The last profile overrides conflicting values.
7. Profile-Specific YAML Configuration
spring:
profiles:
active: dev
---
spring:
config:
activate:
on-profile: dev
server:
port: 8081
---
spring:
config:
activate:
on-profile: prod
server:
port: 8080
8. Profile-Based Logging
# application-dev.properties
logging.level.root=DEBUG
# application-prod.properties
logging.level.root=ERROR
9. Profiles with Spring Boot Tests
@SpringBootTest
@ActiveProfiles("test")
public class UserServiceTest {
}
This loads the application-test.properties configuration.
10. Common Use Cases
- Database configurations
- Logging levels
- Security rules
- Mock vs real services
- Feature toggles
11. Best Practices
- Do not hard-code credentials
- Use environment variables in production
- Use @Profile for beans, not business logic
- Avoid too many profiles
- Prefer YAML for complex configuration
12. Interview Questions (Quick)
- Can we have multiple active profiles? Yes
- What is the default profile? default
- Can profiles control bean loading? Yes, using @Profile
- Spring vs Spring Boot profiles? Same concept, Boot simplifies usage
13. Real-World Analogy
- Same car
- Different driving modes (Eco, Sport, Test)
Summary
Spring Boot profiles are essential for managing environment-specific configurations. They make applications safer, cleaner, and easier to deploy across multiple environments.