Recall & Review
beginner
What is field injection in Spring Boot?
Field injection is when dependencies are injected directly into class fields using annotations like @Autowired on private variables.Click to reveal answer
beginner
Why is field injection discouraged in Spring Boot?
Field injection hides dependencies, makes testing harder, and breaks immutability because fields are set after object creation.
Click to reveal answer
beginner
What is a better alternative to field injection?
Constructor injection is better because it makes dependencies explicit, supports immutability, and improves testability.
Click to reveal answer
intermediate
How does field injection affect unit testing?
Field injection makes it hard to provide mock dependencies in tests because fields are private and set by the framework after object creation.
Click to reveal answer
beginner
What is one real-life analogy for field injection drawbacks?
It's like getting ingredients for a recipe delivered inside the cooking pot after you start cooking, making it hard to prepare and test the recipe properly.
Click to reveal answer
Which annotation is commonly used for field injection in Spring Boot?
✗ Incorrect
@Autowired is used to inject dependencies directly into fields.
Why is constructor injection preferred over field injection?
✗ Incorrect
Constructor injection clearly shows dependencies and allows creating immutable objects.
What problem does field injection cause for unit testing?
✗ Incorrect
Field injection hides dependencies inside private fields, complicating mock setup.
Which of these is NOT a reason to avoid field injection?
✗ Incorrect
Field injection hides dependencies; making them explicit is a benefit of constructor injection.
What is a simple way to fix field injection issues?
✗ Incorrect
Constructor injection solves many problems caused by field injection.
Explain what field injection is and why it is generally discouraged in Spring Boot.
Think about how dependencies are set and how that affects testing and design.
You got /5 concepts.
Describe the benefits of constructor injection compared to field injection.
Constructor injection makes dependencies clear from the start.
You got /4 concepts.