Bird
0
0

How can you programmatically check which Spring profile is currently active inside a Spring Boot component?

hard📝 Application Q9 of 15
Spring Boot - Docker and Deployment

How can you programmatically check which Spring profile is currently active inside a Spring Boot component?

ACheck system environment variables directly
BInject <code>Environment</code> and call <code>getActiveProfiles()</code>
CRead <code>application.properties</code> file manually
DUse <code>@Profile</code> annotation on the component
Step-by-Step Solution
Solution:
  1. Step 1: Recall Spring's Environment abstraction

    Spring provides an Environment object to access active profiles programmatically.
  2. Step 2: Use getActiveProfiles() method

    Calling getActiveProfiles() on Environment returns the list of active profiles.
  3. Final Answer:

    Inject Environment and call getActiveProfiles() -> Option B
  4. Quick Check:

    Use Environment.getActiveProfiles() to check profiles [OK]
Quick Trick: Inject Environment and call getActiveProfiles() [OK]
Common Mistakes:
  • Using @Profile annotation to check active profiles
  • Manually reading properties files
  • Directly accessing system env vars without Spring

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes