Bird
0
0

You want to inject two beans of the same type into a Spring Boot service using constructor injection. How can you distinguish them?

hard📝 Application Q8 of 15
Spring Boot - Inversion of Control and Dependency Injection
You want to inject two beans of the same type into a Spring Boot service using constructor injection. How can you distinguish them?
AUse @Autowired on only one constructor
BUse @Qualifier annotation on constructor parameters
CRename one bean class to a different name
DUse field injection instead of constructor injection
Step-by-Step Solution
Solution:
  1. Step 1: Understand multiple beans of same type problem

    Spring cannot decide which bean to inject if multiple beans of the same type exist.
  2. Step 2: Use @Qualifier to specify bean names

    @Qualifier annotation on constructor parameters tells Spring exactly which bean to inject.
  3. Final Answer:

    Use @Qualifier annotation on constructor parameters -> Option B
  4. Quick Check:

    @Qualifier resolves multiple bean injection conflicts [OK]
Quick Trick: Use @Qualifier to specify which bean to inject [OK]
Common Mistakes:
  • Thinking renaming classes solves injection conflict
  • Believing @Autowired alone resolves ambiguity
  • Switching injection type unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes