Click Here for Our Recommended Antivirus for Your Device

Difference Between Constructor Injection and Setter Injection

When dependency resolution is made by an external agent rather than the class itself, then it is known as dependency injection.


Science Quiz

Test your knowledge about topics related to science

1 / 10

The substances that enter a chemical reaction are called __________.

2 / 10

What is laughing gas?

3 / 10

Quartz crystals normally used in quartz clocks etc. is chemically

4 / 10

Where does photosynthesis take place?

5 / 10

The purpose of choke in tube light is?

6 / 10

Galvanised iron sheets have a coating of

7 / 10

What is the scientific name of humans?

8 / 10

Fermentation is the process of ______.

9 / 10

Which of the following gland is present in the human mouth?

10 / 10

Name the metal which is most ductile?

Your score is


There are two standard ways of dependency injection that are being supported in the Spring framework, through a constructor argument or via using the setter method.

The former type of dependency injection is known as the constructor injection, and the latter one is known as the setter injection. Both the methods are used for different purposes and have their own pros and cons.

Constructor Injection vs Setter Injection 

The difference between Constructor injection and Setter injection is that constructor injection uses constructor, and on the other hand, setter injection uses setter methods to inject dependency. These two types of injection ways differ from one another on the basis of several parameters like security, readability, supporting immutability, and so on.

Constructor Injection vs Setter Injection

Want to save this article for later? Click the heart in the bottom right corner to save to your own articles box!

Constructor injection is a type of dependency injection in the spring framework that uses a constructor to inject dependency. The dependencies that are required for any of the classes are specified as the parameters of that specific class’s constructor.

Constructor injection does not allow the developer to construct any object unless all the dependencies are ready, and thus, it ensures successful dependency injection.

Setter injection is a type of dependency injection in the spring framework that uses setter methods to inject dependency. It has setter methods of the form setXYZ() where XYZ denotes a dependency that injects the dependent objects into the client.

This way of resolving dependency is very common in the Spring framework.

Comparison Table

Parameters of ComparisonConstructor InjectionSetter Injection 
Dependency injectedIt uses a constructor.It uses setter methods.
ReadabilityIt is not much readable in comparison to the setter.It is more readable.
Override propertyCannot override dependency.Can override certain dependencies.
ChangesIt always creates a new bean instance.It does not create any new bean instance.
ImmutabilityIt supports immutability.It does not support.

What is Constructor Injection?

Constructor injection is one of the standard ways of resolving dependency within the Spring framework, which statically defines the list of dependencies required for classes by assigning those as the parameters to the class’s constructor.

All the classes that require dependency are must have a public constructor inside them, which takes an instance as the constructor argument, and that constructor should necessarily be the only public constructor inside that class.

If there is a need for more than one dependency, then the additional arguments should be added to the same constructor. To ensure that the dependency is injected completely, a single responsibility principle should be followed, which states that members should do only one thing.

The constructor should be kept free from any other logic to make the constructor of the classes faster and more reliable.

One important fact of constructor injection is that the objects won’t be constructed unless all the dependencies are ready. As it creates new instances every time the constructor is being called thus, overriding is not possible in it.

Constructor injection is basically used in cases when there is a necessity of creating objects with all of the dependencies. This is the most generally applicable and easiest way to implement dependencies correctly.

constructor injection

What is Setter Injection?

Setter injection is one of the standard ways of resolving dependency within the Spring framework, which uses the setter methods to resolve the dependencies. The setter methods are of the form setXYZ(), where XYZ specifies the dependency that needs to be injected.

In this type of injection method, first, the object is created, and later on, the dependency is injected. For configuring Spring, XML files are used, and thus, readability is a big concern.

As setter methods have a specified form of method names, it enhances the readability in many ways. When there is a need to inject a larger number of dependencies, then the setter injection type is very favored and preferred over other types of injection methods.

In addition to that, by using setter injection, the developers can easily override and change the values because it does not create a new bean instance every time. The only drawback of setter injection is that it does not ensures complete dependency injection.

There is no guarantee as to whether a certain object has dependency injected or not. In other words, it means that there might be an object with incomplete dependency. Setter injection is the most flexible and the most common way of implementing dependency injection.

setter injection

Main Differences Between Constructor Injection and Setter Injection 

  1. When constructor injection is used to inject the dependency, it is done by using constructor on any Spring-managed bean, whereas the Setter injection uses setter methods like set dependency() for the purpose of injecting dependency on any of the bean-managed by the Spring’s IOC container.
  2. Setter injection is more readable in comparison to the constructor injection as the setter method always has names starting with the word set like setXYZ() and thus, it is easy to read in the Spring XML config file and interpret the dependency that is being set whereas the Constructor injection uses an index for the purpose of injecting dependency.
  3. Certain dependencies can be overridden using the setter injection, whereas this is not possible by using the constructor injection as every time a new object is created when the constructor is called.
  4. By using the setter injection, the value can easily be changed as it does not create any new bean instances, whereas that is not the case while using the constructor injection because it always creates a new bean instance.
  5. Constructor injection supports immutability, whereas, on the other hand, setter injection does not support immutability.
One request?

I’ve put so much effort writing this blog post to provide value to you. It’ll be very helpful for me, if you consider sharing it on social media or with your friends/family. SHARING IS ♥️

Leave a Comment

Your email address will not be published. Required fields are marked *