Bird
0
0

How can you define a Spring bean that should only be loaded when either the 'dev' or 'test' profile is active?

hard📝 Application Q8 of 15
Spring Boot - Spring Annotations
How can you define a Spring bean that should only be loaded when either the 'dev' or 'test' profile is active?
A@Profile("dev & test")
B@Profile({"dev", "test"})
C@Profile("dev | test")
D@Profile("dev-test")
Step-by-Step Solution
Solution:
  1. Step 1: Understand @Profile syntax

    The @Profile annotation accepts an array of profile names, meaning the bean is active if any of the listed profiles is active.
  2. Step 2: Analyze options

    @Profile({"dev", "test"}) uses an array with "dev" and "test", which means the bean is active if either profile is active. @Profile("dev & test") uses an invalid syntax with '&' which means both profiles simultaneously, which is not supported. @Profile("dev | test") uses a string with '|', which is not valid syntax. @Profile("dev-test") uses a hyphen which is not a recognized operator.
  3. Final Answer:

    @Profile({"dev", "test"}) -> Option B
  4. Quick Check:

    Array syntax activates bean for any listed profile [OK]
Quick Trick: Use array syntax in @Profile for multiple profiles [OK]
Common Mistakes:
  • Using logical operators like '&' or '|' inside the string
  • Assuming a single string with comma-separated profiles works
  • Using unsupported characters like hyphens

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes