logo

Difference Between Constructor And Setter Dependency Injection


Show

Dependency Injection is a way to pass dependent objects to other objects. There are two standard ways of dependency injection that come in the framework of Spring. These are Constructor injection and Setter injection. In Constructor based dependency injection, a dependency is injected through a constructor argument. On the other hand, a Setter injection uses a setter method to identify the dependency between the objects. Both the methods are used for different purposes and have their own advantages and disadvantages.

In the Setter method, we will use setXXX() for e.g; and in Constructor based dependency we will use XXX as a constructor argument to find the dependency.

Constructor Injection vs Setter Injection

The key difference between Constructor injection and Setter injection in Spring is that in constructor injection, we use constructor, and on the other hand, setter injection uses setter methods to inject dependency. Both the methods will help in finding the dependency but differ in terms of parameters like security, readability, immutability, and many other issues.

In Constructor injection, we use an argument to inject the dependency. Here, the dependencies are defined as the parameters of that specific class’s constructor. It is a successful dependency injection method because it will not allow the developer to construct any object unless all the dependencies are injected.

On the contrary, a Setter dependency injection method uses setter methods to inject dependency. For example, it uses setXXX() where XXX is identified as a dependency that injects the dependent objects. It is a very common dependency injection method used in the Spring framework.

In the Setter dependency injection, there exists a strategy of creating the bean first but it will do the injection just before the use of the bean by the setter methods according to your configuration. In case, if you miss any bean to inject in the configuration, the injection will be stopped for those beans and the dependent beans will not perform accordingly when you are using those. We will require the assurance from the Inversion of control (IoC) container that the injection of necessary beans must be done before using any bean.

On the other hand, In constructor-based dependency injection, it must be necessary for the container to provide the dependencies properly while constructing the bean. This is also termed as "container-agnostic manner", we need to provide dependencies while creating the bean making the identification of dependency, independent of any IoC container.

Comparison Table Between Setter Injection and Constructor Injection

Now will create the comparison table between the Setter Dependency injection and Constructor Dependency injection in Spring.

Setter Dependency injection

Constructor Dependency injection

In the Setter injection, partial dependency: can be injected. If there are three properties, three arg constructor and setters methods exist in a class, then it is possible to pass information for only one property by using the Setter method.

Not possible in the case of Constructor Dependency injection.

Since the Setter injection overrides the constructor injection, the IoC container will use the Setter injection if both injections are used simultaneously.

Constructor Dependency injection cannot override the Setter injection as a result it is not preferred by the IoC Container to use in case both the injections are used simultaneously.



We can easily change the values by setter injection because It doesn't create a new bean instance.

You will have to create a new bean instance in case of value changes.

Setter injection is flexible

Constructor Dependency injection is not so flexible.


Readability is good.

Readability is not good as compared to its setter counterpart.

Setter injection will not support immutability.

It supports immutability.

Setter Dependency supports circular dependency.


Constructor Dependency doesn’t support circular dependency.

It is helpful to use Setter dependency injection as it helps to inject the dependency only when it is required.


Constructor injection will create the order in which the dependencies need to be injected.

There is no added benefit in setter injection


Constructor injection provides extra safety in a multithreaded environment.

Spring code generation library supports Setter based Injection

It is good to use for optional dependencies

Constructor-based Injection is not supported by the Spring code generation library.

It is not so good for optional dependencies, so it should be used for mandatory dependencies.

It supports Partial dependency injection so that we can ignore any property for injection. The injection of properties is done only if necessary.

Partial dependency injection is not supported by Constructor injection. In this dependency method, all the parameters of the constructor must be injected.

While using Setter injection, a default constructor is necessary.


Here, the default constructor is not mandatory.

Setter injection is less secure when compared with constructor injection as it allows you to override a dependency by the overridden setter methods.

It is more secure than setter injection as dependency cannot be overridden. The sub-classes need to call super class constructors in Constructor injection.

In Setter injection, if two objects are dependent on each other, then circular dependency is not effective as objects use setter methods of others.

In Constructor injection, if two objects are dependent on each other, then circular dependency is effective as one object has to wait for another object to create. Spring will throw the exception.

Different Scenarios of Using Setter and Constructor Injection in Spring

  • If there is a sole property in a spring bean class, then it is preferred to use constructor injection as constructor executes before methods. Constructor injection is faster than setter injection.
  • On the contrary, if the spring bean class is having many properties then it is a good choice to use setter injection as programmers need not worry about the index, type of parameters, etc; in constructor injection. It helps the programmer ease the burden.
  • If it is not compulsory for all the properties to be configured then Constructor injection will be the preferred choice as properties can be left with their default values.

An Example of Constructor Injection will Look Like This:

public class ConstructorInjectionExample {
   public ConstructorInjectionExample(BaseExmp baseExmp) {
      // ...
   }
}
<beans>
   <bean id = "ConstructorInjectionExample" class = "x.y.ConstructorInjectionExample">
   <constructor-arg ref = "baseExmp"/>
</bean>
<bean id = "baseExmp" class = "x.y.BaseExmp"/>
</beans>

An Example of Setter Injection is as Follows:

public class SetterInjectionExample {
   public void setBaseExmp(BaseExmp baseExmp) {
      this.baseExmp = baseExmp;
   }
}

<beans>
   <bean id = "setterInjectionExample" class = "x.y.SetterInjectionExample">
   <property name = "baseExmp" ref = "baseExmp"/>
</bean>
</beans>

Both ways of injecting dependencies (Constructor and Setter) let you find the dependencies between the objects but they have their own advantages and drawbacks as we hope you find in this guide. You are free to use both constructor injection and setter injection in the Spring configuration file.