permaculture playing cards*
The moose likes Spring and the fly likes Difference Between Constructor and Setter Injection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Difference Between Constructor and Setter Injection" Watch "Difference Between Constructor and Setter Injection" New topic

Difference Between Constructor and Setter Injection

jinesh parikh
Ranch Hand

Joined: Apr 20, 2007
Posts: 48

I was reading this post on why people use setter injection more than constructor injection.But I could not understand the first reason as well.

Spring maintained its focus on setter injection because at the time, we believed that the lack of default arguments and argument names for constructor arguments resulted in less clarity for developers

can somebody please explain me the first reason stated above?

Any help would be appreciated.

Bill Gorder

Joined: Mar 07, 2010
Posts: 1648

The reference you quoted is simply stating that constructor args do not have names in the spring configuration XML, which can make it less clear for developers. That said the thread you were looking at is quite old and the new and more preferred approach today is Spring's annotation based field injection using @Autowired, @Inject, and @Resource. In this case setters are not required.

[How To Ask Questions][Read before you PM me]
It is sorta covered in the JavaRanch Style Guide.
subject: Difference Between Constructor and Setter Injection
Similar Threads
Dependency Injection with setter injection ?
DI - Constructor injection / Setter Injection ?
Is not autowire a another type of dependendcy injection?
Dependency checking and constructor injection
Not sure about exact difference between Constructor Injection and Setter Injection