Difference between Setter vs Constructor Injection in Spring

Spring Setter vs. Constructor Injection

Spring supports two types of dependency Injection, using a setter method, e.g. setXXX(), where XXX is a dependency or via a constructor argument. The first way of dependency injection is known as setter injection while later is known as constructor injection. Both approaches of Injecting dependency on Spring bean has there pros and cons, which we will see in this Spring framework article. The difference between Setter Injection and Constructor Injection in Spring is also a popular Spring framework interview question.

Some time interviewer also asks as When do you use Setter Injection over Constructor injection in Spring or simply benefits of using setter vs. constructor injection in Spring framework. Points discussed in this article not only help you to understand Setter vs. Constructor Injection but also Spring's dependency Injection process.

By the way, if you are new in the Spring framework and learning it, you may want to take a look at my list of 5 good books to learn Spring framework. That will certainly help in your learning process. Since Spring is now a must-have skill for Java programmers, it worth putting time and effort to learn this powerful framework.

Difference between Setter and Constructor Injection in Spring framework

Setter vs Constructor Injection in Spring framework - Interview question As I said, earlier Spring supports both setter and constructor Injection, which are two standard ways of injecting dependency on beans managed by IOC constructor. Spring framework doesn't support Interface Injection on which dependency is injected by implementing a particular interface.

In this section, we will see a couple of differences between setter and constructor Injection, which will help you decide when to use setter Injection over constructor Injection in Spring and vice-versa.

1) The fundamental difference between setter and constructor injection, as their name implies, is How dependency is injected.  Setter injection in Spring uses setter methods like setDependency() to inject dependency on any bean managed by Spring's IOC container. On the other hand, constructor injection uses theconstructor to inject dependency on any Spring-managed bean.

2) Because of using the setter method, setter Injection in more readable than constructor injection in Spring configuration file usually applicationContext.xml . Since the setter method has name like setReporotService() by reading Spring XML config file you know which dependency you are setting. While in constructor injection, since it uses an index to inject the dependency, it's not as readable as setter injection and you need to refer either Java documentation or code to find which index corresponds to which property.

3) Another difference between setter vs constructor injection in Spring and one of the drawbacks of setter injection is that it does not ensures dependency Injection. You can not guarantee that certain dependency is injected or not, which means you may have an object with incomplete dependency. On the other hand, constructor Injection does not allow you to construct an object until your dependencies are ready.

4) One more drawback of setter Injection is Security. By using setter injection, you can override certain dependency which is not possible with constructor injection because every time you call the constructor, a new object is gets created.

5) One of our reader Murali Mohan Reddy pointed out one more difference between Setter and Constructor Injection in Spring, where later can help if there is a circular dependency between two object A and B.

If Object A and B are dependent each other i.e A is depends ob B and vice-versa. Spring throws ObjectCurrentlyInCreationException while creating objects of A and B bcz A object cannot be created until B is created and vice-versa. So spring can resolve circular dependencies through setter-injection. Objects constructed before setter methods invoked.

See the comment section for more inputs from other readers.

When to use Setter Injection over Constructor Injection in Spring

Setter Injection has upper hand over Constructor Injection in terms of readability. Since for configuring Spring we use XML files, readability is a much bigger concern. Also, a drawback of setter Injection around ensuring mandatory dependency injected or not can be handled by configuring Spring to check dependency using "dependency-check" attribute of tag or tag.

Another worth noting point to remember while comparing Setter Injection vs Constructor Injection is that once a number of dependencies crossed a threshold like 5 or 6 it's handy manageable to passing dependency via the constructor. Setter Injection is the preferred choice when a number of dependencies to be injected is a lot more than normal, if some of those arguments is optional than using Builder design pattern is also a good option.

In Summary, both Setter Injection and Constructor Injection have there owned advantage and disadvantages. The good thing about Spring is that it doesn't restrict you to use either Setter Injection or Constructor Injection and you are free to use both of them in one Spring configuration file. Use Setter injection when a number of dependencies are more or you need readability. Use Constructor Injection when Object must be created with all of its dependency.

Further Learning

Spring Framework 5: Beginner to Guru

Design Pattern Library

From 0 to 1: Design Patterns - 24 That Matter - In Java

Java Design Patterns - The Complete Masterclass

Other Spring tutorials from Javarevisited Blog

  • Top 5 Spring Boot Features Java developer should know (features)
  • How to setup LDAP authentication in Java using Spring Security? (solution)
  • Top 5 Courses to learn Spring in depth (courses)
  • How to limit the maximum number of concurrent active sessions in the Java web app? (demo)
  • Top 5 Courses to learn Microservices in Java (courses)
  • How to convert ArrayList to delimited String in Java using Spring? (solution)
  • 10 Advanced Spring Boot Courses for Java developers (courses)
  • How to get a ServletContext object in the Spring controller? (example)
  • Top 5 Courses to learn Spring Boot in-depth (courses)
  • 10 example of display tag in JSP and spring (examples)
  • Top 5 Books to learn Spring Boot and Spring Cloud (books)
  • What is the default bean scope in the Spring MVC framework? (answer)
  • Top 5 Courses to learn Spring Cloud for Java developers (courses)

P.S. - If you want to learn how to develop RESTful Web Service using Spring MVC in-depth, I suggest you join the REST with Spring certification class by Eugen Paraschiv. One of the best courses to learn REST with Spring MVC.