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:
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.
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.
Final Answer:
@Profile({"dev", "test"}) -> Option B
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
Master "Spring Annotations" in Spring Boot
9 interactive learning modes - each teaches the same concept differently