Bird
0
0

What happens if a Spring Boot component has multiple constructors but none is annotated with @Autowired?

medium📝 component behavior Q5 of 15
Spring Boot - Inversion of Control and Dependency Injection
What happens if a Spring Boot component has multiple constructors but none is annotated with @Autowired?
ASpring throws a BeanCreationException due to ambiguity
BSpring uses the default no-argument constructor
CSpring picks the constructor with the most parameters
DSpring throws a NoSuchBeanDefinitionException at runtime
Step-by-Step Solution
Solution:
  1. Step 1: Recall Spring's constructor selection rules

    If multiple constructors exist and none is annotated, Spring cannot decide which to use.
  2. Step 2: Identify Spring's error on ambiguity

    Spring throws BeanCreationException due to ambiguous constructors.
  3. Final Answer:

    Spring throws a BeanCreationException due to ambiguity -> Option A
  4. Quick Check:

    Multiple constructors without @Autowired cause ambiguity error [OK]
Quick Trick: Multiple constructors need @Autowired to avoid ambiguity [OK]
Common Mistakes:
  • Thinking Spring picks constructor automatically
  • Assuming default constructor is always used
  • Confusing exception types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes