Profiles: dev / test / prod
In Spring Boot, a profile is a named set of configuration properties that allows your application to behave differently in different environments. For example, your app may need different databases, logging levels, or API keys in development, testing, and production. Profiles make it easy to switch configurations without changing code, keeping environments isolated and safe.
What is a Profile?
- A profile is a way to group configuration settings based on the environment.
- Each profile can have its own configuration file, such as
application-dev.ymlorapplication-prod.yml. - Only the configuration for the active profile is loaded at runtime.
- Profiles help avoid hardcoding environment-specific values in the main configuration file.
Common Profiles
| Profile | Purpose |
|---|---|
| dev | Development environment; enables debugging, local DB, verbose logging |
| test | Testing environment; for integration/unit tests, in-memory DB, test-specific configs |
| prod | Production environment; stable settings, production DB, optimized logging |
Profile-Specific Configuration Files
You can create profile-specific property or YAML files in src/main/resources:
application.properties # Common/default settings
application-dev.yml # Development-specific settings
application-test.yml # Testing-specific settings
application-prod.yml # Production-specific settings
Example: application-dev.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/dev_db
username: dev_user
password: dev_pass
server:
port: 8081
logging:
level:
org.springframework: DEBUG
Example: application-prod.yml
spring:
datasource:
url: jdbc:mysql://prod-server:3306/prod_db
username: prod_user
password: prod_pass
server:
port: 8080
logging:
level:
org.springframework: WARN
Activating a Profile
Spring Boot allows you to activate a profile in multiple ways depending on how and where the application is running. The active profile decides which configuration file is loaded at startup.
Using application.properties
This method is useful when you want to set a default profile for local development. The application will always start with the dev profile unless overridden.
spring.profiles.active=dev
Command Line
This is commonly used in production deployments and CI/CD pipelines. It overrides any profile defined inside configuration files.
java -jar myapp.jar --spring.profiles.active=prod
Environment Variables
This approach is recommended for cloud and container-based deployments (Docker, Kubernetes) because it keeps configuration external and secure.
export SPRING_PROFILES_ACTIVE=test
YAML Example
This is the YAML equivalent of application.properties. It is more readable for complex configurations and works well when using application.yml.
spring:
profiles:
active: dev
Using Profiles in Code
You can load beans conditionally based on the active profile:
@Configuration
@Profile("dev")
public class DevConfig {
// Dev-specific beans
}
@Configuration
@Profile("prod")
public class ProdConfig {
// Prod-specific beans
}
This ensures environment-specific beans are only loaded when needed.
Best Practices
- Keep common settings in
application.propertiesorapplication.yml. - Place environment-specific overrides in profile-specific files (
application-dev.yml,application-prod.yml). - Avoid storing sensitive production credentials in source code; use environment variables or secret managers.
- Use
devfor local development,testfor CI/CD pipelines, andprodfor live production. - Always test configurations in each profile before deployment.
Conclusion
Spring Boot profiles allow you to:
- Switch configurations dynamically without changing code.
- Maintain environment isolation for safer deployments.
- Organize environment-specific settings efficiently.
- Integrate seamlessly with
application.propertiesorapplication.yml.
Profiles are a best practice in professional Spring Boot applications, ensuring scalability, maintainability, and reliability across all environments.
