Bird
0
0

In a Spring Boot application, you have two beans of the same type but want to inject one based on a runtime condition. How can you combine @Qualifier with Spring's @Profile annotation to achieve this?

hard📝 Application Q9 of 15
Spring Boot - Inversion of Control and Dependency Injection
In a Spring Boot application, you have two beans of the same type but want to inject one based on a runtime condition. How can you combine @Qualifier with Spring's @Profile annotation to achieve this?
AAnnotate each bean with @Profile for different environments and use @Qualifier to inject the correct one
BUse @Qualifier only and ignore @Profile
CUse @Profile on the injection point instead of beans
DUse @Primary on both beans and @Qualifier on injection
Step-by-Step Solution
Solution:
  1. Step 1: Use @Profile to define beans for different environments

    Annotate each bean with @Profile("dev"), @Profile("prod"), etc., to activate based on runtime environment.
  2. Step 2: Use @Qualifier to specify which bean to inject

    Use @Qualifier with the bean name to inject the correct bean matching the active profile.
  3. Final Answer:

    Annotate each bean with @Profile for different environments and use @Qualifier to inject the correct one -> Option A
  4. Quick Check:

    @Profile + @Qualifier = conditional bean injection [OK]
Quick Trick: Combine @Profile on beans and @Qualifier on injection [OK]
Common Mistakes:
  • Using @Profile on injection point instead of beans
  • Ignoring @Profile and expecting runtime switching
  • Using @Primary on multiple beans causing conflicts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes