Constructor Dependency Injection in Spring

Constructor dependency injection occurs when a component’s dependencies are provided to it in its constructor (or constructors). The component declares a constructor or a set of constructors, taking as arguments its dependencies, and the IoC container passes the dependencies to the component when instantiation occurs, as shown in the following code snippet:

public class ConstructorInjection {
    private Dependency dependency;
    public ConstructorInjection(Dependency dependency) {
        this.dependency = dependency;
    public String toString() {
        return dependency.toString();
An obvious consequence of using constructor injection is that an object cannot be created without its dependencies; thus, they are mandatory.

Check out a complete example at Spring Dependency Injection via Constructor Example

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course