How to determine programmatically the current active profile using Spring boot
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Programmatically Determining the Active Profile
To determine the currently active profile programmatically, you can use various methods provided by the Spring framework.
Using Environment
Spring's Environment interface is a central part of the configuration model. It provides access to the profiles and properties.
Conditional Profile Printing
You can utilize ApplicationListener to print active profiles upon application startup. This can be useful for logging purposes.
Don't forget to register ActiveProfileLogger as a bean or with the SpringApplication.
Using @Value
You can inject the profile information directly into your beans using the @Value annotation:
Summary of Methods
| 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}") |
Additional Considerations
Using Default Profiles
If no profile is set, you may want to specify a default profile. This can be done with the spring.profiles.default property.
Managing Multiple Profiles
Profiles can be combined by separating them with commas. For example, activating both dev and debug:
Testing Profile-specific Beans
Spring's @ActiveProfiles annotation allows you to specify which profiles should be active when running integration tests.
Conclusion
Determining the active profile in a Spring Boot application is a crucial task that can be achieved using various methods. Whether through environment injection, application listeners, or direct property injection, knowing which profile is active can significantly aid in managing environment-specific configurations. By leveraging these techniques, you can write more adaptive and environment-aware Spring Boot applications.
Related reading
- How to determine programmatically the current active profile using Spring boot
- 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?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.