Newsletter

Difference between Setter Injection and Constructor Injection

Spring » on Aug 17, 2011 { 24 Comments } By Sivateja

Up to now we came through the concept about setter injection and constructor injection with dependencies right, now let us see the difference between setter and constructor injection…

 

Setter InjectionConstructor Injection
1. In Setter Injection, partial injection of dependencies can possible, means if we have 3 dependencies like int, string, long, then its not necessary to inject all values if we use setter injection. If you are not inject it will takes default values for those primitives1. In constructor injection, partial injection of dependencies cannot possible, because for calling constructor we must pass all the arguments right, if not so we may get error
2. Setter Injection will overrides the constructor injection value, provided if we write setter and constructor injection for the same property [i already told regarding this, hope you remember ]2. But, constructor injection cannot overrides the setter injected values
3. If we have more dependencies for example 15 to 20 are there in our bean class then, in this case setter injection is not recommended as we need to write almost 20 setters right, bean length will increase.3. In this case, Constructor injection is highly recommended, as we can inject all the dependencies with in 3 to 4 lines [i mean, by calling one constructor]
4. Setter injection makes bean class object as mutable [We can change ]4. Constructor injection makes bean class object as immutable [We cannot change ]

 
Actually we may have some more differences, but these are what i found upto my knowledge πŸ™‚

​ ​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

24 Responses to “Difference between Setter Injection and Constructor Injection”
  1. Alita Gherardini says:

    Great post, I think people should learn a lot from this weblog its real user friendly. So much good information on here :D.

  2. sankar says:

    ” Setter injection makes bean class object as mutable [We can change ]”
    please provide explanation for it

    • Manisha says:

      We can easily change the value by setter injection. It doesn't create a new bean instance always like constructor. So setter injection is flexible than constructor injection.

  3. Java4s says:

    @sankar

    Hey am really sorry, i just overlooked your comment.

    Actually with setter injection we can assign the values again, but constructor injection will be executed only once, some object creation time.

    Some how we can do something with setter injection even after creation of the object. But its not the case in constructor injection.

    Hope you got it.

  4. Bikash says:

    Hi,
    one more difference is through constructor we can assigned the value to final field variable ,but with setter we can’t.

  5. I always choose setter one because of readability. This link also has some good points

  6. Hi,
    please you provide Spring MVC3.1

  7. venkat says:

    The Setter injection uses tag to inject bean values .
    The Constructor Injection uses tag to inject bean values .

  8. Datta says:

    I think short but sweet…

  9. amitesh says:

    Great post. I appreciate the tremendous effort made in this blog. This is a big contribution. If there is anything I would like to suggest, it would be language improvement.

  10. Neelakanta Reddy says:

    Hi,
    Two more difference are:
    1)Through constructor we can assigned the value to final field variable ,but with setter we can’t.
    2)Through setter injection, we can over come circular dependency. But with constructor we can’t.

  11. Punnaiah Dammu says:

    Nice explanation.

  12. ankit says:

    Lessons are too good except your english.

  13. Sanjay says:

    Can we specify data type, when we are passing value in setter injection?

  14. Siva says:

    GOOD EXPLANATION FOR EACH TOPIC.THANK U!

  15. ramanareddy sagili says:

    nice explanation from java4 guys

  16. Abhishek Kumar says:

    What is the meaning of this please provide the explanation of in real thing example:
    Setter Injection will overrides the constructor injection value, provided if we write setter and constructor injection for the same property

  17. CNU says:

    Nice dude….

  18. Yuvashree says:

    Please provide Explanation for the below differences.

    1)Through constructor we can assigned the value to final field variable ,but with setter we can’t.
    2)Through setter injection, we can over come circular dependency. But with constructor we can’t.

    • Vijay says:

      Hi Yuvashree:
      Please look into the explanation below for both the points :
      1) When you use constructor injection, the parameters you give will be assigned to that object while creating. Now if you wish to change them, you can't do that as you will be creating the new object by calling the constructor injection. But for setter injection, it is not necessary that you provide all the information i.e. dependencies while creating the object. If you call a method setXXX() initially doesn't mean you can not call it again, which means you are just calling the same method for the same instance and hence changing the field values for the same object.

      2) Circular dependency means two objects mutually dependent on each other to create the object. Below is the example I have found will make you understand it better :

      <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>

      Here both the objects are waiting till the other object is created.
      But in case of setter injection, the object is already created and we just have to pass on the dependencies by calling the setter methods.

      Hope this helps. Correct me if any mistakes found.

  19. Karan Mhetre says:

    Short and Sweet explanation!

    @Neelakanta Reddy :

    This is really very good point.

  20. brij says:

    We can easily change the value by setter injection. It doesn't create a new bean instance always like constructor. So setter injection is flexible than constructor injection.

  21. Devi says:

    Could you please explain the line 4 in setter and constructor injection

  22. chandu says:

    Nice explanation everyone can understand..

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.