How to Activate Profile in Spring Boot: Simple Guide
In Spring Boot, activate a profile by setting
spring.profiles.active in application.properties or application.yml, or by passing --spring.profiles.active=profileName as a command line argument. You can also set it as an environment variable SPRING_PROFILES_ACTIVE before running your app.Syntax
To activate a Spring Boot profile, use the spring.profiles.active property. This can be set in configuration files, command line, or environment variables.
- In application.properties:
spring.profiles.active=profileName - In application.yml:
spring.profiles.active: profileName - Command line:
--spring.profiles.active=profileName - Environment variable:
SPRING_PROFILES_ACTIVE=profileName
properties
spring.profiles.active=dev
Example
This example shows how to activate the dev profile using application.properties and how Spring Boot loads the matching configuration.
java
src/main/resources/application.properties: spring.profiles.active=dev src/main/resources/application-dev.properties: app.message=Hello from Dev Profile // Java main class import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } @RestController class MessageController { @Value("${app.message}") private String message; @GetMapping("/") public String getMessage() { return message; } }
Output
Hello from Dev Profile
Common Pitfalls
Common mistakes when activating profiles include:
- Setting
spring.profiles.activein multiple places causing conflicts. - Misspelling profile names, so Spring Boot cannot find the matching config.
- Forgetting to create profile-specific config files like
application-dev.properties. - Using uppercase letters in profile names inconsistently.
Always verify the active profile by checking logs or using Environment bean.
properties
/* Wrong: conflicting profiles in properties and command line */ // application.properties spring.profiles.active=dev // Command line java -jar app.jar --spring.profiles.active=prod /* Right: set profile in only one place to avoid confusion */ // Prefer command line or environment variable for overrides java -jar app.jar --spring.profiles.active=prod
Quick Reference
| Method | How to Set | Usage |
|---|---|---|
| application.properties | spring.profiles.active=profileName | Default profile activation |
| application.yml | spring.profiles.active: profileName | YAML config format |
| Command line | --spring.profiles.active=profileName | Override at runtime |
| Environment variable | SPRING_PROFILES_ACTIVE=profileName | Set in OS environment |
Key Takeaways
Set spring.profiles.active to activate a profile in Spring Boot.
Use application.properties, command line, or environment variables to specify the active profile.
Avoid conflicting profile settings in multiple places.
Profile names are case-sensitive and must match config files.
Check logs or Environment bean to confirm the active profile.