Author
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.
application-dev.yml or application-prod.yml.| 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 |
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
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
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.
application.properties or application.yml.application-dev.yml, application-prod.yml).dev for local development, test for CI/CD pipelines, and prod for live production.Spring Boot profiles allow you to:
application.properties or application.yml.Profiles are a best practice in professional Spring Boot applications, ensuring scalability, maintainability, and reliability across all environments.