Recall & Review
beginner
What is constructor injection in Spring Boot?
Constructor injection is a way to provide dependencies to a class by passing them through its constructor. Spring Boot automatically calls the constructor and injects the required beans.Click to reveal answer
intermediate
Why is constructor injection preferred over field injection in Spring Boot?
Constructor injection is preferred because it makes dependencies explicit, helps with immutability, supports easier testing, and avoids issues with uninitialized fields.
Click to reveal answer
intermediate
How does Spring Boot know which constructor to use for injection?
Spring Boot uses the constructor with the most parameters that can be satisfied by beans in the context. If there is only one constructor, it uses that by default.
Click to reveal answer
beginner
Show a simple example of constructor injection in a Spring Boot service class.
import org.springframework.stereotype.Service;
@Service
public class MyService {
private final MyRepository repo;
public MyService(MyRepository repo) {
this.repo = repo;
}
// service methods
}
Click to reveal answer
intermediate
What happens if a required dependency is missing when using constructor injection?
Spring Boot will fail to start and throw an error because it cannot find a bean to inject into the constructor parameter.
Click to reveal answer
Which of these is a benefit of constructor injection in Spring Boot?
✗ Incorrect
Constructor injection makes dependencies explicit and supports immutability, unlike field injection.
How does Spring Boot choose which constructor to use for injection?
✗ Incorrect
Spring Boot uses the constructor with the most parameters that can be satisfied by beans.
What happens if a dependency is missing for constructor injection?
✗ Incorrect
Missing dependencies cause Spring Boot to fail startup with an error.
Which annotation is required on the constructor for injection in Spring Boot 2.6+?
✗ Incorrect
If there is only one constructor, Spring Boot injects dependencies without needing @Autowired.
Constructor injection helps with which of the following?
✗ Incorrect
Constructor injection supports immutability by setting dependencies once at creation.
Explain constructor injection in Spring Boot and why it is preferred over other injection methods.
Think about how dependencies are passed and why that helps code quality.
You got /5 concepts.
Describe what happens if Spring Boot cannot find a bean to inject into a constructor parameter.
Consider how Spring Boot handles required dependencies.
You got /3 concepts.