Bird
0
0

You want to create a bean that only loads when test profile is active and another bean that loads for all profiles except test. Which annotation setup is correct?

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

You want to create a bean that only loads when test profile is active and another bean that loads for all profiles except test. Which annotation setup is correct?

A@Profile("test") on first bean, no annotation on second bean
BNo need for @Profile, beans load automatically
C@Profile("test") on first bean, @Profile("prod") on second bean
D@Profile("test") on first bean, @Profile("!test") on second bean
Step-by-Step Solution
Solution:
  1. Step 1: Use @Profile("test") for test-only bean

    This ensures the first bean loads only when the test profile is active.
  2. Step 2: Use @Profile("!test") for all except test

    The negation !test means the second bean loads when test is not active.
  3. Final Answer:

    @Profile("test") on first bean, @Profile("!test") on second bean -> Option D
  4. Quick Check:

    Use @Profile("!profile") to exclude a profile [OK]
Quick Trick: Use @Profile("!test") to exclude test profile [OK]
Common Mistakes:
  • Not using @Profile on second bean causes it to always load
  • Using @Profile("prod") limits second bean to prod only
  • Assuming beans load automatically without profiles

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes