Bird
0
0

Consider this Spring Boot code snippet:

medium📝 Debug Q14 of 15
Spring Boot - Inversion of Control and Dependency Injection
Consider this Spring Boot code snippet:
@Component
public class ServiceA {
  @Autowired
  private ServiceB serviceB;
}

@Component
public class ServiceB {
  @Autowired
  private ServiceA serviceA;
}

What problem will occur when the application starts?
AServiceB will not be injected into ServiceA
BNo problem, both services work fine
CCircular dependency causing startup failure
DCompilation error due to missing constructors
Step-by-Step Solution
Solution:
  1. Step 1: Identify circular dependency

    ServiceA depends on ServiceB and ServiceB depends on ServiceA, creating a circular reference.
  2. Step 2: Understand Spring's behavior on circular dependencies

    Spring cannot resolve this circular injection automatically with field injection, causing startup failure.
  3. Final Answer:

    Circular dependency causing startup failure -> Option C
  4. Quick Check:

    Circular dependency = startup error [OK]
Quick Trick: Check if two beans inject each other directly [OK]
Common Mistakes:
  • Assuming circular dependencies always work
  • Thinking missing constructors cause compile errors here
  • Believing Spring resolves all circular injections automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes