Bird
0
0

What is the error in the following code snippet?

medium📝 Debug Q14 of 15
Spring Boot - Inversion of Control and Dependency Injection
What is the error in the following code snippet?
@Component("serviceA")
class ServiceA implements Service {}

@Autowired
@Qualifier("serviceB")
private Service service;
ANo bean named 'serviceB' found for injection
BMissing @Component annotation on Service interface
CIncorrect use of @Autowired without @Qualifier
DService interface cannot be injected
Step-by-Step Solution
Solution:
  1. Step 1: Check available beans

    Only a bean named "serviceA" is defined; no bean named "serviceB" exists.
  2. Step 2: Effect of @Qualifier("serviceB")

    @Qualifier("serviceB") asks Spring to inject a bean named "serviceB", which is missing, causing an error.
  3. Final Answer:

    No bean named 'serviceB' found for injection -> Option A
  4. Quick Check:

    @Qualifier name must match existing bean [OK]
Quick Trick: Bean name in @Qualifier must exist in context [OK]
Common Mistakes:
  • Assuming interface needs @Component
  • Ignoring missing bean name mismatch
  • Thinking @Autowired alone resolves ambiguity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes