Get all updates of Java4s
Follow Us: Email Newsletter

Difference between Setter Injection and Constructor Injection

Spring » On Aug 17, 2011 { 20 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 Injection Constructor 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 primitives 1. 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 Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

20 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

  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.

  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.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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