Friday, May 31, 2024

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.

Btw, In order to best understand design patterns, you need to work out some scenarios, examples, etc. It's best to get this kind of knowledge as part of your work but even if you don't get there, you can supplement them by doing projects and object-oriented software design exercises




Difference between Setter and Constructor Injection in Spring framework

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 the constructor 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.

Difference between Setter vs Constructor Injection in Spring Framework





When to use Setter Injection over Constructor Injection in Spring

Setter vs Constructor Injection in Spring framework - Interview questionSetter 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 handily 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 are optional than using a Builder design pattern is also a good option.


In Summary, both Setter Injection and Constructor Injection have their own advantages 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. 

You should 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.


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)

26 comments:

  1. One of the principle which I follow to choose between Setter Injection vs Constructor Injection is principle of mandatory and optional, yeah It's What I call it :). If any dependency is mandatory for an Object to perform its duty then you should use Constructor Injection, having said that optional dependency can be injected using Setter injection. This technique gets best from both Setter and Constructor injection and its also good from maintenance point of view as it clearly high light which dependency is critical for object to work.

    ReplyDelete
  2. One more drawback is circular dependency when using constructor injection. Appreciated if you can add to your article.

    ReplyDelete
  3. @Molluru, Sure, but can you please elaborate this for our readers?

    ReplyDelete
  4. @Javin, Sure

    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.

    ReplyDelete
  5. Thanks Murali, That make sense, but circular dependency is a not a good idea, and one should remove it altogether. I will include your comment in this article.

    ReplyDelete
  6. can any one exaplain point no-3 i.e how setter injection is that it does not ensures dependency Injection??

    ReplyDelete
  7. Once number of dependency crossed a threshold e.g. 5 or 6, it's NOT handy manageable to passing dependency via constructor. OR Once number of dependency crossed a threshold e.g. 5 or 6, it's handy manageable to passing dependency via constructor? Which one is true I mean If we have more dependencies,then which one is suitable - Setter DI & Constructor DI

    ReplyDelete
  8. Hi
    I have the same questions which are raised by sarty and Rambabu Posa..
    Could you please explain those..!

    ReplyDelete
  9. @Rambabu Posa and @Anonymous, once the number of dependency increases, it's become messy to pass them via constructor based injection. A better approach is to divide those dependency into two group of mandatory and optional, provide mandatory dependency via constructor injection and optional via setter based injection. One disadvantage of using setter-based dependency injection is that programmer is expected to take the responsibility of knowing the order on which dependency needs to be provided. In other word, setter-based dependency injection introduce risk of leaving object in half configured state.

    ReplyDelete
  10. The one and only reason for using Constructor injection is that it make dependency explicit and obvious. You know that in order to use a particular class, what other class you need to create. This makes it more testable as it's easy to mock a class which is required in constructor. On the other hand, when you use setter injection, dependency are some what hidden, it's not that obvious as it is in case of constructor, because constructor is historically used to create object. Now, it will not work well if you don't have a proper dependency injection constructor, otherwise any time you want to use a class, you need it's dependency to create it first. In short, use constructor injection to make obvious what dependency a class need to get it job done. For example

    OrderProcess{

    OrderProcessor(OrderQueue queue){
    .....
    }

    }

    here it's obvious that OrderProcessor needs an OrderQueue to work. In order to test this class, you can provide a mock of OrderQueue.

    ReplyDelete
  11. Partial dependency: can be injected using setter injection but it is not possible by constructor. Suppose there are 3 properties in a class, having 3 arg constructor and setters methods. In such case, if you want to pass information for only one property, it is possible by setter method only.

    ReplyDelete
  12. did not get the Circular dependency matter.. how setter injection resolve this??

    ReplyDelete
  13. @Somitra, In case of circular dependency, spring doesn't let you to create object using constructor exception, but it does with setter injection.

    ReplyDelete
  14. @Javin you have mentioned "setter injection is that it does not ensures dependency Injection" my question is why?

    ReplyDelete
  15. "setter injection is that it does not ensures dependency Injection" can you give more explanation on this point?

    ReplyDelete
  16. @Chiranjeevi, Setter injection relies on calling setXXX() method, so if client doesn't call them then class will be without that dependency. Constructor injection makes it mandatory to supply dependency. It will not let you to create class without those dependencies.

    ReplyDelete
  17. But I couldn't understand how the circular dependency raises please provide small xml example

    ReplyDelete
  18. @Anonymous, circular dependency happens when you have two beans A and B and both are dependent on each other e.g.









    Here A depends upon B and B depends upon A, so a cycle is created.

    ReplyDelete
  19. <bean id="A" class="my.company.A">
    <property name="dependency" ref="B"/>
    </bean>

    <bean id="B" class="my.company.B">
    <property name="dependency" ref="A"/>
    </bean>

    ReplyDelete
  20. In the post its said that in constructor injection everytime its called a new object is created and so no overiding and security threat. But in case of singleton bean even if the constructor is called more than one time will not be same object referred and overiding will happen? Pls help on this.

    ReplyDelete
  21. One more difference,

    if you mismatch the order in .xml file with the parameterized constructor argument order. it will reverse the data.

    Example:
    config.xml







    Employee.java

    public Employee(String ename,String role, Address address){
    this.ename=ename;
    this.role=role;
    this.address=address;

    }


    here the argument ename and role can be mismatched. and ename can have 'tech arch' value and
    role can have 'resource1' value. this can happen if the developer mistakenly enter the entries in config.xml in a wrong order.

    ReplyDelete
  22. Hello @Vincent, blogger eat your XML, you need to escape the XML before pasting but your point is correct. The order is very important while using constructor reference so chances of error is more, but with setter injection you specify the dependency name, hence less error prone.

    ReplyDelete
  23. if dependendenies is mandatory to execute then use construtor injetion.

    ReplyDelete
  24. Everybody knows these points. Actual scenarios when to use setter and constructor DI is missing in content.

    ReplyDelete
  25. Really? Anyway, if it's not clear, let me reiterate again
    1. Use Constructor Injection for all mandatory dependency like Logger so that object is always initialized in right state.
    2. Use Setter injection for optional dependencies.
    3. If number of dependencies are more than 5 or 6 use builder instead of constructor injection.

    ReplyDelete