How to get the active profile programmatically in Spring Boot
Codemia Knowledge Hub ·
Inject Spring's Environment and call environment.getActiveProfiles(), which returns the active profiles as a String[]. You can also log the profiles at startup with an ApplicationListener, or inject spring.profiles.active with @Value. Profiles hold environment-specific configuration such as dev and prod, and checking which one is active helps with debugging, logging and conditional configuration.
How Spring profiles work
Spring Profiles allow you to define different beans and configurations for different environments. A typical use case is having a dev profile for development, and a prod profile for production, each with distinct configurations.
Activating Profiles
Profiles are activated via:
- Properties Files: Define in
application.propertiesorapplication.yml. - Command-Line Arguments: Use
--spring.profiles.active. - Environment Variables: Set
SPRING_PROFILES_ACTIVE.
Example configuration
Each profile can have its own properties file, such as application-dev.properties and application-prod.properties:
Reading the active profiles with Environment
Spring's Environment interface is the center of the configuration model and exposes both profiles and properties. Inject it and call getActiveProfiles():
Logging the active profiles at startup
An ApplicationListener can print the active profiles when the application starts, which helps with logging:
ApplicationEnvironmentPreparedEvent fires before the application context exists, so a listener declared as a bean never receives it. Register ActiveProfileLogger with SpringApplication.addListeners(...) or in META-INF/spring.factories.
Injecting the profile with @Value
You can also inject the property directly into a bean:
Which method to use
| Method | Description | Implementation Example |
Environment Interface | Access profiles via Spring's environment injection. | environment.getActiveProfiles() |
ApplicationListener | Log profiles during application startup. | Implement listener for ApplicationEnvironmentPreparedEvent |
@Value Annotation | Directly inject active profile as a property. | @Value("${spring.profiles.active:default}") |
Default profiles, multiple profiles and tests
Setting a default profile
When no profile is set, spring.profiles.default decides which one applies:
Activating several profiles
Separate profiles with commas, for example to activate both dev and debug:
Choosing profiles in integration tests
The @ActiveProfiles annotation sets which profiles are active while integration tests run:
Related reading
- How to determine the size of an object in Java
- How to determine the version of Gradle?
- How to directly initialize a HashMap (in a literal way)?
- How to disable all Kafka related auto configuration from yaml/properties file in spring-boot-2 without removing dependencies?
- How to disable csrf in Spring using application.properties?
- How to disable Eureka and Spring Cloud Config in a WebMvcTest?
- How to disable flyway in a particular Spring profile?
- How to disable Hibernate validation in a Spring Boot project
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.